Kendo datepicker在Kendo网格列中禁用日期(剑道网格中的日期范围)

时间:2015-01-15 09:57:41

标签: jquery kendo-ui kendo-grid kendo-datepicker

我正在使用kendogrid来填充一些具有日期字段的数据。我需要在绑定到kendogrid时限制数据源中的日期。一个例子如下所示。在这里,我需要在BirthDate字段中禁用1910年1月25日之前的所有日期(例如)。



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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:4)

您应该为BirthDate列定义一个编辑器函数,用于初始化KendoDatePicker并设置最小值。

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)
    });
}

您的代码段如下:

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>