0

I have a date picker jquery function

$("#txtFrom").datepicker({
  beforeShowDay: $.datepicker.noWeekends,
  numberOfMonths: 1,
  minDate: sprint_select(),
  maxDate: $( '#d_id' ).val(),
  onSelect: function (selected) {
    var dt = new Date(selected);
    $("#txtTo").datepicker("option", "minDate", dt);
  }
});

Here i set minDate: sprint_select(), calling function sprint_select.So

function sprint_select(){
  var r_id=$('#r_id').val();
  var dataString = { r_id: r_id}

  $.ajax({
    url: "/manage/scrum/getsprintprd",
    method: "POST",
    data: dataString,
    //                   dataType: "json",
    //cache: false,
    success: function (data)
    {
      //var values= $.trim(data).split("-");
      var values1 = '2018-11-09'.split("-"); 
      var parsed_date = new Date(values[0], values[1]-1, values[2]);
      return parsed_date;
    }
  });
}

here i return parsed_date successful.but no change in calendar min date.Any help would be appreciated.

Mohammad
  • 21,175
  • 15
  • 55
  • 84
Shanu k k
  • 1,235
  • 2
  • 18
  • 43
  • You have different things that go wrong here, i suggest you take a look to [this topic about how to return value from async function](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) (an ajax call is async). So, in simple, your `sprint_select()` hasn't answered yet when datepicker is created, and actually it doesn't answer because the `return` in succes function returns for `success` and not for `sprint_select`. – Kaddath Nov 12 '18 at 09:44
  • Add your `datepicker` creation in ajax `success handler` – Sumesh TG Nov 12 '18 at 09:45
  • 2
    You could set value of `minDate` within `success` callback of the AJAX request. – Alexander Nov 12 '18 at 09:51
  • @Alexander thanks i got it – Shanu k k Nov 12 '18 at 09:58

2 Answers2

0

Try Calling Your datepicker function on sprint_select() success: (because Getting date from api may take time. before that your datepicker function is getting completed.)

    function sprint_select(){
      var r_id=$('#r_id').val();
      var dataString = { r_id: r_id}

      $.ajax({
        url: "/manage/scrum/getsprintprd",
        method: "POST",
        data: dataString,
        //                   dataType: "json",
        //cache: false,
        success: function (data) {
          //var values= $.trim(data).split("-");
          var values1 = '2018-11-09'.split("-"); 
          var parsed_date = new Date(values[0], values[1]-1, values[2]);

           $("#txtFrom").datepicker({
           beforeShowDay: $.datepicker.noWeekends,
           numberOfMonths: 1,
           minDate: parsed_date ,
           maxDate: $( '#d_id' ).val(),
           onSelect: function (selected) {
           var dt = new Date(selected);
           $("#txtTo").datepicker("option", "minDate", dt);
         }
       });

    }
 });
}
Sindhoor
  • 514
  • 3
  • 14
0

Try this,
maxDate like:

<input type="hidden" id="d_id" name="d_id" value="01-28-2019">

script

var max = new Date($( '#d_id' ).val());
$("#txtFrom").datepicker({
    beforeShowDay: $.datepicker.noWeekends,
    numberOfMonths: 1,
    minDate: sprint_select(),
    maxDate: max,
    onSelect: function (selected) {
       var dt = new Date(selected);
       $("#to_date").datepicker({
          minDate: dt,
       });
    }
});

Check Ajax return value it should be in mm/dd/yyyy format

function sprint_select(){
    var parsed_date = new Date('01-02-2019');
    return parsed_date; 
}
Darpan Patel
  • 387
  • 3
  • 15