问题
I have an array of objects, what is the best way to determine if a date is in the array so it cannot be selected in the jQuery date picker. If needed, I can store DateValue as a date.
var EventDates = [{"DateID: "1", DateValue: "3/1/2011", FormattedDate: Tue, Mar 1 2011"},
{"DateID: "2", DateValue: "3/2/2011", FormattedDate: Wed, Mar 2 2011"}]
$('.juidateicon_ext').datepicker({
showOn: "button",
buttonImage: "/Content/images/icons/calendar.gif",
buttonImageOnly: true,
showOn: 'both',
beforeShowDay: eventDays
});
function eventDays(date) {
1. Code to determine if "date" is in DateValue in the EventDates Array
2. If date exist, return [false, ""];else return [true, ""];
}
回答1:
I took the liberty of cleaning up your EventDates
array:
var EventDates = [{
"DateID": "1",
"DateValue": "3/1/2011",
"FormattedDate": "Tue, Mar 1 2011"},
{
"DateID": "2",
"DateValue": "3/2/2011",
"FormattedDate": "Wed, Mar 2 2011"
}];
And here's the eventDays
function:
function eventDays(date) {
var length = EventDates.length, i = 0;
var event = null, eventDate = false;
while (i < length && !eventDate) {
event = EventDates[i];
eventDate = new Date(event.DateValue).valueOf() === date.valueOf();
i++;
}
return [!eventDate, ''];
}
Here's a working example: http://jsfiddle.net/andrewwhitaker/uYpnW/1/
Hope that helps!
来源:https://stackoverflow.com/questions/5167732/excluding-dates-in-the-jquery-datepicker-using-beforeshowday-and-an-array-of-obj