Vishnu Vikraman Vishnu Vikraman - 1 month ago 24
jQuery Question

Kendo datepicker disable dates in Kendo grid column (Date range in kendo grid)

I am using a kendogrid which populate some data which have a date field. I need to restrict the dates in the datasource when it binds to kendogrid. An example is shown below. Here I need to disable all the dates before 25-Jan-1910(for example) in the BirthDate field.



var data = createRandomData(50);
var autoCompleteDS = new kendo.data.DataSource({
data: [
{firstName: "Alex"},
{firstName: "Alice"},
{firstName: "Antony"},
{firstName: "Anne"},
{firstName: "Anna"}
]
});

$(document).ready(function() {
$("#grid").kendoGrid({
dataSource: {
data: data,
schema: {
model: {
fields: {
FirstName: { type: "string" },
LastName: { type: "string" },
City: { type: "string" },
Title: { type: "string" },
BirthDate: { type: "date" },
Age: { type: "number" }
}
}
},
pageSize: 10
},
height: 450,
scrollable: true,
sortable: true,
pageable: true,
editable: true,
edit: function(e) {
$(e.container)
.find("input[name='FirstName']")
.data("kendoAutoComplete")
.bind("change", function(e) {
console.log("auto complete changed");
});
},
toolbar: ["create"],
columns: [
{
field: "FirstName",
title: "First Name",
// width: 100
},
{
field: "BirthDate",
title: "Birth Date",
template: '#= kendo.toString(BirthDate,"MM/dd/yyyy") #'
},
{
field: "Age",
width: 100
},

]
});
});

<link href="http://cdn.kendostatic.com/2012.2.710/styles/kendo.default.min.css" rel="stylesheet"/>
<link href="http://cdn.kendostatic.com/2012.2.710/styles/kendo.common.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script src="http://cdn.kendostatic.com/2012.2.710/js/kendo.all.min.js"></script><script src="http://demos.telerik.com/kendo-ui/content/shared/js/people.js"></script>
<!-- grid element -->
<div id="grid" style="width: 700px; margin: 0 auto;"></div>




Answer

You should define an editor function for BirthDate column that initializes the KendoDatePicker and set the minimum value.

editor: function(container, options) {
    $('<input data-text-field="' + options.field + '" data-value-field="' + options.field + '" data-bind="value:' + options.field + '" data-format="' + options.format + '"/>')
    .appendTo(container)
    .kendoDatePicker({
         min: new Date(1910, 0, 25)
    });
}

Your code snippet would be something like:

var data = createRandomData(50);
var autoCompleteDS = new kendo.data.DataSource({
  data: [
    {firstName: "Alex"},
    {firstName: "Alice"},
    {firstName: "Antony"},
    {firstName: "Anne"},
    {firstName: "Anna"}
  ]
});

$(document).ready(function() {
  $("#grid").kendoGrid({
    dataSource: {
      data: data,
      schema: {
        model: {
          fields: {
            FirstName: { type: "string" },
            LastName: { type: "string" },
            City: { type: "string" },
            Title: { type: "string" },
            BirthDate: { type: "date" },
            Age: { type: "number" }
          }
        }
      },
      pageSize: 10
    },
    height: 450,
    scrollable: true,
    sortable: true,
    pageable: true,
    editable: true,
    edit: function(e) {
      $(e.container)
      .find("input[name='FirstName']")
      .data("kendoAutoComplete")
      .bind("change", function(e) {
        console.log("auto complete changed");
      });
    },
    toolbar: ["create"],
    columns: [
      {
        field: "FirstName",
        title: "First Name",
        width: 100
      },
      {
        field: "BirthDate",
        title: "Birth Date",
        width: 100,
        format: "{0:MM/dd/yyyy}",
        parseFormats: [ "MM/dd/yyyy" ],
        editor: function(container, options) {
          $('<input data-text-field="' + options.field + '" data-value-field="' + options.field + '" data-bind="value:' + options.field + '" data-format="' + options.format + '"/>')
          .appendTo(container)
          .kendoDatePicker({
            min: new Date(1910, 0, 25)
          });
        }
      },
      {
        field: "Age",
        width: 100
      }
    ]
  });
});
html {
  font-size: 11px;
}
<link href="http://cdn.kendostatic.com/2014.3.1314/styles/kendo.default.min.css" rel="stylesheet"/>
<link href="http://cdn.kendostatic.com/2014.3.1314/styles/kendo.common.min.css" rel="stylesheet"/>
<script src="http://cdn.kendostatic.com/2014.3.1314/js/jquery.min.js"></script>
<script src="http://cdn.kendostatic.com/2014.3.1314/js/kendo.all.min.js"></script><script src="http://demos.telerik.com/kendo-ui/content/shared/js/people.js"></script>
<!-- grid element -->
<div id="grid" style="width: 700px; margin: 0 auto;"></div>