2

我有一个需要选择日期和日期的要求,以便日期应该在网格中匹配并显示结果。但是带有日期的网格列包含日期+时间。所以我的问题是可以忽略时间并只读日期在往返日期选择器中并匹配网格中的值。

$("#datepicker").kendoDatePicker({
      dateFormat: "dd-mm-yy", 
format    : "dd-MM-yy "});


$("#datepicker1").kendoDatePicker({
  dateFormat: "dd-mm-yy", 
format    : "dd-MM-yy "});


 var result=$("#grid").kendoGrid({
    filterable: true,


  columns: [
    { field: "FirstName", title: "First Name" },
    { field: "LastName", title: "Last Name" },
       { field: "dob", title: "DOB" ,  format: "{0:dd-MM-yy 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-05-30 21:56:15" },
        { FirstName: "Jane", LastName: "Smith" ,dob:"2013-05-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-03-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;
        }
        }   

  }
}).data("kendoGrid");


$("#datepicker, #datepicker1").on("change", function (){
    var mindate = $('#datepicker').data("kendoDatePicker").value();
    var maxdate = $('#datepicker1').data("kendoDatePicker").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);
});

这是我用往返日期更新剑道网格过滤器的小提琴

4

1 回答 1

3

定义一个包含日期但没有时间的附加列,并使用此列进行过滤。

这可以在您的data函数中完成:

data: function (data) {
    $.each(data, function (i, val) {
        val.dob2 = kendo.parseDate(val.dob, "yyyy-MM-dd");
        val.dob = kendo.parseDate(val.dob, "yyyy-MM-dd HH:mm:ss");
    });
    return data;
}

和过滤部分:

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

第二个选项是将过滤条件定义为:

if (mindate !== null) {
    condition.filters.push({ field: "dob", operator: "ge", value: new Date(mindate) });
}
if (maxdate !== null) {
    condition.filters.push({ field: "dob", operator: "le", value: new Date(maxdate) });
}

我们在其中添加一个new Date(...)用于时间添加到日期的位置。

注意:这在您进行客户端过滤时是有效的。如果您正在执行服务器端过滤,则应轻松更改服务器中的查询以处理此问题。

于 2013-04-03T12:15:39.127 回答