How to disable past dates without hiding them in K

2020-02-14 05:20发布

问题:

If I set min value for the date picker, it does not display dates less than min date when it's opened.

My requirement is that dates less than min date should be shown in the date picker, but they should be disabled.

回答1:

You can make it with CSS styles and using custom content in Kendo datepicker.

HTML:

<input id="datepicker" style="width:150px;" />

CSS:

.disabledDay { 
    display: block;
    overflow: hidden;
    min-height: 22px;
    line-height: 22px;
    padding: 0 .45em 0 .1em;
    cursor:default;
    opacity: 0.5;
}

JavaScript:

$(document).ready(function() {

disabledDaysBefore = [
  +new Date("10/20/2014")
];

var p = $("#datepicker").kendoDatePicker({
      value: new Date(),
      dates: disabledDaysBefore,
      month: {
          content: '# if (data.date < data.dates) { #' +    
          '<div class="disabledDay">#= data.value #</div>' +
          '# } else { #' +
          '#= data.value #' +
          '# } #'
      },
      open: function(e){
          $(".disabledDay").parent().removeClass("k-link")
          $(".disabledDay").parent().removeAttr("href")
      },
     }).data("kendoDatePicker");

});

See demo: JSFIDDLE



回答2:

All credit to devlero on this one, I was able to convert this to Razor Syntax, if anyone would like to use that instead.

@(Html.Kendo().DatePicker()
          .Name("datepicker")
          .Value(DateTime.Today)
          .Events(e => e.Open("onOpen"))
          .MonthTemplate("# if (data.date < disabledDaysBefore) { #" +
                                "<div class='disabledDay'>#= data.value #</div>" +
                             "# } else { #" +
                "#= data.value #" +
                "# } #")
          .HtmlAttributes(new { style = "width: 150px;" })      
    )


 $(document).ready(function () {                         
        disabledDaysBefore = [
          +new Date("10/20/2014")
        ];      
    });

function onOpen() {
            $(".disabledDay").parent().removeClass("k-link")
            $(".disabledDay").parent().removeAttr("href")
        }