StuBlackett
StuBlackett

Reputation: 3857

jQuery Ui beforeShowDay - Disable Certain Dates

I am using an Ajax call to PHP to return a string of dates that a user is available, To make it dynamic.

I am returning the string back to my jQuery as :

['7-15-2013','7-16-2013','7-17-2013','7-18-2013','7-19-2013','7-20-2013','7-21-2013','7-22-2013']

However when I initialise the datePicker, That string of dates does not load.

I am not too sure as to why, I suspect its my success function after my Ajax call.

My jQuery code looks like this :

$('.vendor_id').change(function()
    {
        var vendor_id = $('option:selected', this).attr('title');

        var url = "/admin/po/get_user_dates/" + vendor_id;

        $.ajax({
            type : "POST",
            url  : url,
            success: function(unavaildates)
            {
                var disabledDays = unavaildates;

                function disableAllTheseDays(date) {
                    var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
                    for (i = 0; i < disabledDays.length; i++) {
                        if($.inArray((m+1) + '-' + d + '-' + y,disabledDays) != -1) {
                            return [false];
                        }
                    }
                    return [true];
                }


                $('.booking-date').datepicker(
                        {
                            beforeShowDay: disableAllTheseDays,
                            numberOfMonths: 2,
                            dateFormat: 'DD, d MM, yy',
                            showButtonPanel: true
                        }
                )

            }
        })
    })

Hope someone call help me on this.

Quick Update.. On assigning disabledDays to ['7-15-2013','7-16-2013','7-17-2013','7-18-2013','7-19-2013','7-20-2013','7-21-2013','7-22-2013'] the datePicker works...?

Thanks in advance.

Upvotes: 0

Views: 832

Answers (1)

sdespont
sdespont

Reputation: 14025

Because you are retrieving a string and not an array from the server.

I would suggest to return the string without square brakets "'7-15-2013','7-16-2013','7-17-2013','7-18-2013','7-19-2013','7-20-2013','7-21-2013','7-22-2013'" from the server and create the array like this :

var unavaildates = "'7-15-2013','7-16-2013','7-17-2013','7-18-2013','7-19-2013','7-20-2013','7-21-2013','7-22-2013'"
var disabledDays = unavaildates.split(',');

Upvotes: 2

Related Questions