Excluding dates in the jQuery datepicker using beforeShowDay and an array of objects

旧时模样 提交于 2019-12-25 00:08:47

问题


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

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