jquery UI datepicker - disabling date ranges

房东的猫 提交于 2019-12-04 14:03:28

Quite an old question, but I got through the same problem, landed here searching for a solution and then resolved by myself paying more attention to the documentation:

beforeShowDay - function(date) The function takes a date as a parameter and must return an array with [0] equal to true/false indicating whether or not this date is selectable, 1 equal to a CSS class name(s) or '' for the default presentation, and [2] an optional popup tooltip for this date. It is called for each day in the datepicker before it is displayed.

So, just to be sure, I would rewrite the orginal question's code explicitely like this:

$('#cottageCal').datepicker({
    minDate: '0d',
    maxDate: lastDate,
    beforeShowDay: function(date) {
        $.each(cottageStart, function(key, value) { 
            if ((date >= value) && (date <= value)) {
                return new Array(false, 'booked');
            } else {
                return checkPeak(date);
            }
        });
    }
});

and this:

var checkPeak = function(date) {
    var day = date.getDay();
    var month = date.getMonth();
    var returnArr = new Array();
    $.each(peakStart, function(key, value) { 
        if ((date > value) && (date < value)) {
        /* december peak bookings on monday*/
            if (month != 11) {
                returnArr[0] = (day == 5);
                returnArr[1] = '';
                return returnArr;
            } else {
                returnArr[0] = (day == 1);
                returnArr[1] = '';
                return returnArr;
            }
        }
        if (month == 0) {
            returnArr[0] = false;
            return returnArr;
        } else {
            // it's not during a peak period
            returnArr[0] = (day == 1 || day == 5);
            returnArr[1] = '';
            return returnArr;
        }
    });
}

It worked fine for me!

@Penzizzle

what minimumDate u needed?? 0d is not valid in minDate:

either set minDate:'-1d'

see reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!