Help! How to make days enabled in UI datepicker after month change?

后端 未结 1 1882
长情又很酷
长情又很酷 2021-01-25 19:36

In my situation:

I got the days which want to be enabled from ajax called. When I change the month ,I read the xml file vial ajax called and got the days of that month.H

1条回答
  •  走了就别回头了
    2021-01-25 19:58

    The problem is the date format here, when you're storing the dates they're coming out as 2010-1001 instead of 2010-10-01, so change this:

    $daysWithRecords.push(dateToFind+$(this).text());
    

    To this:

    $daysWithRecords.push(dateToFind+"-"+$(this).text());
    

    You can see it working here.


    Here's an overall more optimized version as well, less looping and no infinitely growing array:

    var daysWithRecords = [];
    
    function initDaysArray( $xml , year , month ) {
      var d = year+'-'+month;
      daysWithRecords = 
        $xml.find('user[id="126"] whDateList[month="'+d+'"] date').map(function() {
          return d+"-"+$(this).text();
        }).get();
    }
    
    function checkAvailability(availableDays) {
        var checkdate = $.datepicker.formatDate('yy-mm-dd', availableDays);
        for(var i = 0; i < daysWithRecords.length; i++) { 
           if(daysWithRecords[i] == checkdate){
               return [true, "available"];
            }
        }
        return [false, ""];
    }
    
    $('#div').datepicker({ 
      dateFormat: 'yy-mm-dd',
      defaultDate: '2010-09-01', 
      onChangeMonthYear: getDays, 
      beforeShowDay: checkAvailability
    });
    

    You can test it here.

    0 讨论(0)
提交回复
热议问题