0

I have a problem while disabling some dates in jQuery datepicker. I am successfully retrieving dates from database in a string array inside webmethod. Now through a java script function i am trying to disable these dates. There is another datepicker on the page which is also not showing because of this.

$.ajax({
        type: "POST",
        url: "attendentry.aspx/GetALLPresentsDates",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            var result = msg.d;
            var unavailableDates = [];
            var len = result.length;
            for (var i = 0; i < len; i++) {
                unavailableDates.push(result[i]);
            }

        },
        error: function (msg) {
        }
    }); 

    function unavailable(date) {
    //    var unavailableDates = ["9-17-2013", "9-20-2013", "10-1-2013"]
    var  dmy = (date.getMonth() + 1) + "-" + date.getDate() + "-" + date.getFullYear();
        if ($.inArray(dmy, unavailableDates) == -1) {
            return [true, ""];
        } else {
            return [false, "", "Unavailable"];
        }
    }

    $(function () {
        $("#datepicker1").datepicker({
            minDate: '-30',
            maxDate: '0',
            dateFormat: 'mm-dd-yy',
            beforeShowDay: unavailable,
            onSelect: function (dateText, inst) {
                $("#<%=txtPresent.ClientID%>").val(dateText);
            }
        });
          });

WebeMethod is:

[WebMethod]
public static string[] GetALLPresentsDates()
{
    DataTable dt = new DataTable();
    List<String> dates = new List<String>();
    SqlConnection con = new SqlConnection(Helper.ConnectionString);
    SqlCommand cmd = new SqlCommand("SELECT Emp_PresentDate FROM Emp_Attendance", con);
    SqlDataAdapter da;
    da = new SqlDataAdapter(cmd);
    dt.Clear();
    da.Fill(dt);
    for (int i = 0; i < dt.Rows.Count - 1; i++)
    {
        dates.Add((String.Format("{0:M-d-yyyy}", dt.Rows[i][0])));
    }

    return dates.ToArray();
}

1 Answer 1

0

Update your unavailable function with this. Assuming your date format returned from service is m-d-yy, without leading zero for month and day. If it is not the case, you need to update the formatDate argument ('m-d-yy') with the correct format.

function unavailable(date) {
    return [ unavailableDates.indexOf(jQuery.datepicker.formatDate('m-d-yy', date)) === -1 ];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.