是否有任何解决方案可以匹配剑道日期选择器值和网格日期列值

时间:2013-04-03 10:44:00

标签: kendo-ui

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

$("#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);
});

这是我更新Kendo grid filter with to and from dates

的小提琴

1 个答案:

答案 0 :(得分: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(...)

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