Javascript NoGray Calendar add number of days to d

2019-08-18 04:11发布

I previously asked a similar question at this location, Javascript NoGray Calendar find next available date after chosen blocked dates if today is blocked, for the check the a single calendar. Now for the second calendar, I am trying to make it check the previous date at my_cal1, add a number of days, which I have selected as 7 days, and then check if that date is available and if not to do the same thing as the first calendar by checking each day after until a date it available and then to select that date. So to make clear, the number of days from the first calendar selected date is not fixed completely, only if the date it selects is available and if not, it moves to the next available date.

Here is the code I am trying so far. It's the second onLoad, the onLoad for calendar 2 my_cal2 that I am trying to resolve:

    my_cal1 = new ng.Calendar({
    input: {date:'date1', month:'month1', year:'year1'},
    selected_date:new Date(),
    display_date:new Date(),
    dates_off:[{date:1, month:0},{date:25, month:11},{date:24, month:6, year:2014},{date:27, month:6, year:2014},{date:28, month:6, year:2014},{date:29, month:6, year:2014},{date:30, month:6, year:2014},{date:31, month:6, year:2014},{date:8, month:7, year:2014},{date:9, month:7, year:2014}],
    events: {
        onDateClick: function(dt){
            this.select_date(dt);
        },
        // code to check if the start date is selectable
        onLoad: function(){
            var st_dt = this.get_start_date().clone();
            while(!this.is_selectable(st_dt)[0]){
                st_dt = st_dt.from_string('today + 1');
            }
            // checking if no dates are selected
            if (!ng.defined(this.get_selected_date())){
                this.select_date(st_dt);
            }
            this.set_start_date(st_dt);
        }
    }
});


    my_cal2 = new ng.Calendar({
    input: {date:'date1', month:'month1', year:'year1'},
    selected_date:new Date(),
    display_date:new Date(),
    dates_off:[{date:1, month:0},{date:25, month:11},{date:24, month:6, year:2014},{date:27, month:6, year:2014},{date:28, month:6, year:2014},{date:29, month:6, year:2014},{date:30, month:6, year:2014},{date:31, month:6, year:2014},{date:8, month:7, year:2014},{date:9, month:7, year:2014}],
    events: {
        onDateClick: function(dt){
            this.select_date(dt);
        },
        // code to check if the start date is selectable
        onLoad: function(){
                var st_dt = this.get_start_date().clone();
                console.log(this.is_selectable(st_dt)[0]);
                while(!this.is_selectable(st_dt)[0])
                {
                    st_dt = st_dt.from_string('today + 1');
                }
                // checking if no dates are selected
                if (!ng.defined(this.get_selected_date()))
                {
                    var stt_dt = this.select_date(st_dt.from_string('today + 7')).clone();
                    console.log(this.is_selectable(stt_dt)[0]);
                    while(!this.is_selectable(stt_dt)[0])
                    {
                        stt_dt = stt_dt.from_string('today + 1');
                    }
                    if (!ng.defined(this.get_selected_date(stt_dt)))
                    {
                        this.select_date(stt_dt);
                    }
                }
                this.set_start_date(stt_dt);
      }
    }
});

Any help would be much appreciated. Thanks in advance.

UPDATE:

I tried with my_cal2.get_selected_date():

            onLoad: function()
            {
                var theDate = my_cal1.get_start_date();
                theDate = theDate.from_string('today + 7');
                var st_dt = theDate.clone();
                console.log(this.is_selectable(st_dt)[0]);
                while(!this.is_selectable(st_dt)[0])
                {
                    st_dt = st_dt.from_string('today + 1');
                }
                // checking if no dates are selected
                if (!ng.defined(this.get_selected_date()))
                {
                    this.select_date(st_dt);
                }
                this.set_start_date(st_dt);
            },      

This works but now all dates before are unavailable, even if they are not blocked, and also when you use get_day_since to see the difference between the two dates, it does not work properly.

Help still needed. Thanks.

1条回答
Emotional °昔
2楼-- · 2019-08-18 04:40

Yes, I was able to figure out the answer:

                onLoad: function()
            {
                var theDate = my_cal1.get_start_date().clone();
                theDate = theDate.from_string('today + 7');
                var st_dt = theDate.clone();
                console.log(this.is_selectable(st_dt)[0]);
                while(!this.is_selectable(st_dt)[0])
                {
                    st_dt = st_dt.from_string('today + 1');
                }
                // checking if no dates are selected
                if (!ng.defined(this.get_selected_date()))
                {
                    this.select_date(st_dt);
                }
                var origDate = my_cal1.get_start_date();
                this.set_start_date(origDate);
                this.set_selected_date(st_dt);
            },      

Thanks to anyone who was trying to solve this.

查看更多
登录 后发表回答