Search code examples
jqueryajaxweb-servicesjquery-uijquery-ui-datepicker

How to disable days of week in date picker based on response from web service


In my project there is many employees , each one of them has his own schedule per week , I've implemented a web service that returns a list of integers which represents the work days for the specified employee , the main idea for all of this is that I need to prevent users from making appointments in the other days of week .. how to hide those days from the date picker ? or if there is another way to do this ?

web service

[WebMethod]

    public void getWorkDays(int id)
    {
        WorkDays wd = new WorkDays(id);

        List<Days> workDays = new List<Days>();
        String docSch_query = "select * from workDays where empId = " + id + " and status=1";
        DBConnect conn = new DBConnect();
        DataTable dt = conn.select(docSch_query);

        foreach (DataRow r in dt.Rows)
        {
            switch (r["dayId"].ToString())
            {
                case "1":
                    workDays.Add(new Days(1,"sat"));
                    break;
                case "2":
                    workDays.Add(new Days(2, "sun"));
                    break;
                case "3":
                    workDays.Add(new Days(3, "mon"));
                    break;
                case "4":
                    workDays.Add(new Days(4, "tus"));
                    break;
                case "5":
                    workDays.Add(new Days(5, "wed"));
                    break;
                case "6":
                    workDays.Add(new Days(6, "thu"));
                    break;
            } 
        }

        JavaScriptSerializer js = new JavaScriptSerializer();
        Context.Response.Write(js.Serialize(workDays));           
    }

date picker

$(document).ready(function () {

        var dates=$.ajax({
            type: "POST",
            url: "getdays.asmx/getWorkDays",
            data: {id:<%#int.Parse(Session["appWith"].ToString())%>},
            dataType: "json",

            success: function (data) {

                return data.d;
            }
        });

        $('#edob').datepicker({

            firstDay: 3,
            beforeShowDay: function (date) {
                var available = $.inArray(date.getDay(), dates['int']) > -1;

                if (available) {
                    return [1];   //return available date
                }
                else {
                    return [0];   //return not available
                }
            }
        });
    })

Solution

  • I found the solution for my question ..

    $(document).ready(function () {
            var dates = $.parseJSON(
                $.ajax({
                    type: "POST",
                    url: "getdays.asmx/getWorkDays",
                    data: { id: <%=int.Parse(Session["appWith"].ToString())%> },
                    dataType: "json",
                    async: false
                }).responseText);
    
            $('#edob').datepicker({
    
                firstDay: 3,
                beforeShowDay: function (date) {
                    var a =[];
                    var weekend = false;
                    for (i = 0; i < dates.length; i++) {
                        a.push(dates[i].Id);
                    }
                    var weekend = date.getDay() == 5 || date.getDay() == 6 || !($.inArray(date.getDay(),a)>-1);
                    return [!weekend, weekend ? 'myweekend' : ''];
                }
          });
        });