带有日期时间选择器的KendoGrid过滤器无法正常工作?

时间:2013-03-11 11:46:03

标签: javascript visual-studio-2010 kendo-ui

此问题是根据日期时间选择器过滤网格数据(服务数据)。  我无法提供服务所以我在这里使用硬代码数据,但我的需求是基于日期和时间的过滤服务数据。 这是jsbin http://jsbin.com/exakic/41/edit

1 个答案:

答案 0 :(得分:1)

您将parse函数放在grid定义中,而不放在datasource中。您还需要提供与正在接收的格式匹配的格式(“yyyy-MM-dd HH:mm:ss”)。试试这个:

dataSource: {
    data  : [
        { FirstName: "Joe", LastName: "Smith", dob: "2013-02-18 19:54:13"},
        { FirstName: "Jane", LastName: "Smith", dob: "2013-02-18 20:55:14" },
        { FirstName: "Jane", LastName: "Smith", dob: "2013-02-18 21:56:15" },
        { FirstName: "Jane", LastName: "Smith", dob: "2013-02-18 22:57:16" },
        { FirstName: "Jane", LastName: "Smith", dob: "2013-02-19 20:55:20" },
        { FirstName: "Jane", LastName: "Smith", dob: "2013-02-24 20:56:14" },
        { FirstName: "Jane", LastName: "Smith", dob: "2013-02-26 20:57:14" },
        { FirstName: "Jane", LastName: "Smith", dob: "2013-02-28 20:42:14" },
        { FirstName: "Jane", LastName: "Smith", dob: "2013-03-22 11:55:14" },
        { FirstName: "Jane", LastName: "Smith", dob: "2013-03-27 20:55:14" },
        { FirstName: "Jane", LastName: "Smith", dob: "2013-04-18 20:55:14" },
        { FirstName: "Jane", LastName: "Smith", dob: "2013-04-23 20:55:14" },
        { FirstName: "Jane", LastName: "Smith", dob: "2013-04-24 20:55:14" }
    ],
    schema: {
        data: function (data) {
            $.each(data, function (i, val) {
                val.dob = kendo.parseDate(val.dob, "yyyy-MM-dd HH:mm:ss");
            });
            return data;
        }
    }

我还会做一些额外的改动:

  • format中加入DateTimePickers定义以匹配Grid中的格式:

代码:

$("#datetimepicker").kendoDateTimePicker({
    showSecond: true,
    dateFormat: "dd-MM-yy",
    timeFormat: "HH:mm:ss",
    format    : "dd-MM-yy HH:mm:ss"
});

$("#datetimepicker1").kendoDateTimePicker({
    showSecond: true,
    dateFormat: "dd-MM-yy",
    timeFormat: "HH:mm:ss",
    format    : "dd-MM-yy HH:mm:ss"
});
  • 通过blur事件更改change事件,只有在输入字段的值实际发生变化时才会触发它。

代码:

$("#datetimepicker, #datetimepicker1").on("change", function () {
    var mindate = $('#datetimepicker').data("kendoDateTimePicker").value();
    var maxdate = $('#datetimepicker1').data("kendoDateTimePicker").value();
    var condition = {
        logic  : "and",
        filters: [
        ]
    };
    if (mindate !== null) {
        condition.filters.push({ field: "dob", operator: "ge", value: mindate });
    }
    if (maxdate !== null) {
        condition.filters.push({ field: "dob", operator: "le", value: maxdate });
    }
    result.dataSource.filter(condition);
});