Programing

Jquery UI datepicker.

lottogame 2020. 10. 9. 08:44
반응형

Jquery UI datepicker. 날짜 배열 비활성화


내 Jquery ui datepicker 문제에 대한 해결책을 찾으려고 노력했지만 운이 없습니다. 내가하려는 것은 ...

Jquery UI Datepicker에서 차단하려는 JSON 배열을 반환하기 위해 복잡한 PHP를 수행하는 응용 프로그램이 있습니다. 이 배열을 반환합니다.

["2013-03-14","2013-03-15","2013-03-16"]

간단히 말할 수있는 간단한 방법이 없습니까? datepicker에서 이러한 날짜를 차단합니까?

UI 설명서를 읽었는데 도움이되는 내용이 없습니다. 누구나 아이디어가 있습니까?


beforeShowDay사용 하여이 작업 을 수행 할 수 있습니다.

다음 예제는 2013 년 3 월 14 일부터 2013 년 3 월 16 일까지 날짜를 비활성화합니다.

var array = ["2013-03-14","2013-03-15","2013-03-16"]

$('input').datepicker({
    beforeShowDay: function(date){
        var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
        return [ array.indexOf(string) == -1 ]
    }
});

데모 : Fiddle


IE 8에는 indexOf 기능이 없으므로 대신 jQuery inArray를 사용했습니다.

$('input').datepicker({
    beforeShowDay: function(date){
        var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
        return [$.inArray(string, array) == -1];
    }
});

일요일 (또는 다른 요일)과 날짜 배열도 차단하려면 다음 코드를 사용합니다.

jQuery(function($){

    var disabledDays = [
       "27-4-2016", "25-12-2016", "26-12-2016",
       "4-4-2017", "5-4-2017", "6-4-2017", "6-4-2016", "7-4-2017", "8-4-2017", "9-4-2017"
    ];

   //replace these with the id's of your datepickers
   $("#id-of-first-datepicker,#id-of-second-datepicker").datepicker({
      beforeShowDay: function(date){
         var day = date.getDay();
         var string = jQuery.datepicker.formatDate('d-m-yy', date);
         var isDisabled = ($.inArray(string, disabledDays) != -1);

         //day != 0 disables all Sundays
         return [day != 0 && !isDisabled];
      }
   });
});   

$('input').datepicker({
   beforeShowDay: function(date){
       var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
       return [ array.indexOf(string) == -1 ]
   }
});

beforeShowDate 는 나를 위해 작동하지 않았으므로 계속 진행하여 자체 솔루션을 개발했습니다.

$('#embeded_calendar').datepicker({
               minDate: date,
                localToday:datePlusOne,
               changeDate: true,
               changeMonth: true,
               changeYear: true,
               yearRange: "-120:+1",
               onSelect: function(selectedDateFormatted){

                     var selectedDate = $("#embeded_calendar").datepicker('getDate');

                    deactivateDates(selectedDate);
                   }

           });


              var excludedDates = [ "10-20-2017","10-21-2016", "11-21-2016"];

              deactivateDates(new Date());

            function deactivateDates(selectedDate){
                setTimeout(function(){ 
                      var thisMonthExcludedDates = thisMonthDates(selectedDate);
                      thisMonthExcludedDates = getDaysfromDate(thisMonthExcludedDates);
                       var excludedTDs = page.find('td[data-handler="selectDay"]').filter(function(){
                           return $.inArray( $(this).text(), thisMonthExcludedDates) >= 0
                       });

                       excludedTDs.unbind('click').addClass('ui-datepicker-unselectable');
                   }, 10);
            }

            function thisMonthDates(date){
              return $.grep( excludedDates, function( n){
                var dateParts = n.split("-");
                return dateParts[0] == date.getMonth() + 1  && dateParts[2] == date.getYear() + 1900;
            });
            }

            function getDaysfromDate(datesArray){
                  return  $.map( datesArray, function( n){
                    return n.split("-")[1]; 
                });
             }

DD-MM-YY의 경우 다음 코드를 사용하십시오.

var array = [ "03-03-2017 ', '03 -10-2017', '03 -25-2017"]

$('#datepicker').datepicker({
    beforeShowDay: function(date){
    var string = jQuery.datepicker.formatDate('dd-mm-yy', date);
    return [ array.indexOf(string) == -1 ]
    }
});

function highlightDays(date) {
    for (var i = 0; i < dates.length; i++) {
    if (new Date(dates[i]).toString() == date.toString()) {
        return [true, 'highlight'];
    }
}
return [true, ''];
}

jquery datepicker에서 특정 날짜를 비활성화하려면 여기에 간단한 데모가 있습니다.

<script type="text/javascript">
    var arrDisabledDates = {};
    arrDisabledDates[new Date("08/28/2017")] = new Date("08/28/2017");
    arrDisabledDates[new Date("12/23/2017")] = new Date("12/23/2017");
    $(".datepicker").datepicker({
        dateFormat: "dd/mm/yy",
        beforeShowDay: function (date) {
            var day = date.getDay(),
                    bDisable = arrDisabledDates[date];
            if (bDisable)
                return [false, "", ""]
        }
    });
</script>

참고 URL : https://stackoverflow.com/questions/15400775/jquery-ui-datepicker-disable-array-of-dates

반응형