Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass a list of dates to a JQuery calendar [duplicate]

does anyone have any idea how I can pass a list of dates in code behind to a calendar, so it will highlight the dates which are in the list. thank you

    <script>
        $(function () {
        $("#datepicker").datepicker();
        });
    </script>

   <p>Date: <input type="text" id="datepicker" /></p>

list of dates stored in code behind:

DateTime start = new DateTime(2013, 02, 20);
        DateTime end = new DateTime(2013, 01, 11);

        var dates = new List<DateTime>();

        for (var dt = start; dt <= end; dt = dt.AddDays(1))
        {
            dates.Add(dt);
        }
like image 407
John Avatar asked Feb 22 '13 17:02

John


1 Answers

The simplest solution for you would be to create asp.net handler or a web page that returns the dates as json objects and use it on a beforeShowDate function inside the datepicker:

Here is the simple implementation:

var dates = []; //replace [] with ajax call to the page that returns the dates
$("#datepicker").datepicker({
      dateFormat: "dd-mm-yyyy",

      beforeShowDay: function (date) {
          var available = $.inArray(date, dates) > -1;   

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

Code behind would look something like this. You will have to add System.Web.Extensions.dll as a reference to be able to use JavaScriptSerializer

    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime start = new DateTime(2013, 02, 20);
        DateTime end = new DateTime(2014, 01, 11);

        var dates = new List<string>();

        for (var dt = start; dt <= end; dt = dt.AddDays(1))
        {
            dates.Add(dt.ToString("dd-MM-yyyy"));
        }

        Response.ContentType = "text/javascript";
        string json = new JavaScriptSerializer().Serialize(dates);
        Response.Write(json);
        Response.End();
    }`
like image 115
Davor Zlotrg Avatar answered Sep 30 '22 02:09

Davor Zlotrg