问题
I'm struggling to get something working - I'm getting a 'Uncaught TypeError: Cannot read property '0' of undefined' error with the below and I can't figure out why!
I'm trying to use the jQuery UI datepicker on a site for a holiday cottage to indicate availability and season (low, high etc). I have a function as a datePicker event to check if there's a booking for a date, and if not, I then go out to check which season we're in (In non-peak season bookings can be made on a Monday or Friday. In Peak season, it's Fridays only. )
I'm using a cms to generate some date arrays (via loops) that I can then iterate through when building the calendar, so the javascript is a little verbose.
The arrays look like this:
<script>
//Peak Season 1 2011
var ps1 = new Date('June 17, 2011');
var pe1 = new Date('September 2, 2011');
//Peak Season 2 2011
var ps2 = new Date('December 19, 2011');
var pe2 = new Date('January 6, 2012');
// season start and end date arrays
var peakStart = new Array(ps1,ps2);
var peakEnd = new Array(pe1,pe2);
// Bookings
//Mr & Mrs Smith
var cbs1 = new Date('May 27, 2011');
var cbe1 = new Date('June 5, 2011');
//Mr & Mrs Jones
var cbs2 = new Date('September 1, 2011');
var cbe2 = new Date('September 18, 2011');
var cottageStart = new Array(cbs1,cbs2);
var cottageEnd = new Array(cbe1,cbe2);
// last date of season - don't book past here
var lastDate = '01/06/2012';
</script>
I have the following function that I call from the beforeShowDate event to check the calendar date against the booking array:
$('#cottageCal').datepicker({
minDate: '0d',
maxDate: lastDate,
beforeShowDay: function(date) {
$.each(cottageStart, function(key, value) {
//alert (((date >= value) && (date <= value)) ? 'booked' : 'notbooked');
if ((date >= value) && (date <= value)) {
return [false, 'booked'];
} else {
return checkPeak(date);
}
});
}
});
Finally, the checkPeak function that is called from here looks like this:
var checkPeak = function(date) {
var day = date.getDay();
var month = date.getMonth();
$.each(peakStart, function(key, value) {
if ((date > value) && (date < value)) {
/* december peak bookings on monday*/
if (month != 11) {
return [(day == 5), ''];
} else {
return [(day == 1), ''];
}
}
if (month == 0) {
return false;
} else {
// it's not during a peak period
return [(day == 1 || day == 5), ''];
}
});
}
I must be doing something fundamentally wrong here but I can't see what!
回答1:
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!
回答2:
@Penzizzle
what minimumDate u needed?? 0d
is not valid in minDate:
either set minDate:'-1d'
see reference
来源:https://stackoverflow.com/questions/5116121/jquery-ui-datepicker-disabling-date-ranges