JavaScript - Disable calendar dates - Asked By sunny C on 30-Sep-13 05:01 AM

Hi,
I am trying to disable some of the calendar dates which will come from database. I am getting the data from db fine and I can push the data to jQuery js file. But this calendar is being instantiated in document.ready function.

So can you guide me how would I achieve this functionality. If I hard code the values in document.ready function, it is working fine. I need the data to be populated from db.

I am using knockoutjs and jQuery and ASP.NET.

Html:

<input type="text" id="datepicker" readonly="readonly" data-bind="text: contacts.collectionDate" />

JS file:

GlobalVar.datesToDisable = "";
GlobalVar.setDisableDates = function(disableDates) { // Getting the data from db and assigning to variable
    if (disableDates != "") {
        GlobalVar.disableDates = disableDates.split(',');
        console.log(GlobalVar.disableDates);
    }
};

$(function() {
var disabledDates = GlobalVar.disableDates; 
    

    function noWeekendsOrHolidays(date) {
        var noWeekend = $.datepicker.noWeekends(date);
        if (noWeekend[0]) {
            return nationalDays(date);
        } else {
            return noWeekend;
        }
    }

    function nationalDays(date) {
        for (var i = 0; i < disabledDates.length; i++) {
            if (new Date(disabledDates[i]).toString() == date.toString()) {
                return [false];
            }
        }
        return [true];
    }

GlobalVar.menu = function() {
        $("#datepicker").datepicker({
            beforeShowDay: noWeekendsOrHolidays, 
            minDate: +2,
            dateFormat: 'dd/mm/y',
            changeYear: true,
            changeMonth: true            
        });

        
    } ();
});
    
Thanks,
Sunny