[英]Filter Kendo DataSource by date range
我目前正在尝试使用Kendo DropDownList过滤Kendo数据源,但它没有按我预期的那样工作。
我有一个Kendo DropDownList,其中有一些选项,允许用户按1、3、6、9或12个月筛选数据源。 我还使用moment.js
处理到当前日期增加了几个月。
这是我的DropDownList onChange
事件:
onMonthRangeChange: function (e) {
var value = e.sender.value();
switch (value) {
case "1":
var oneMonth = moment().add(1, "month");
var firstOfMonth = moment().startOf("month");
viewModel.myTaskDataSource.query({
logic: "and",
filters: [
{ field: "DueDate", operator: "gte", value: firstOfMonth },
{ field: "DueDate", operator: "lte", value: oneMonth }
]
});
break;
case "3":
// similar to above code except for 3 months
case "6":
// similar to above code except for 6 months
case "9":
// similar to above code except for 9 months
case "12":
// similar to above code except for 12 month
}
})
尽管我认为这会行得通,但行不通。 相反,它仅显示数据源中的所有内容。 我什至尝试将viewModel.myTaskDataSource.query
更改为viewModel.myTaskDataSource.filter
但随后出现以下错误:
未捕获的TypeError:e.indexOf不是函数-kendo.all.js:1129
我还怀疑这可能是因为Kendo和moment.js使用了不同的时间格式,但是即使将所有的kendo日期解析为时刻日期,我也会遇到相同的错误。
经过大量的实验,我终于找到了解决方案!
我更仔细地查看了该错误,并注意到它提到了有关kendo.parseDate
因此我知道日期存在一些问题,导致过滤无法正常工作。
我只是将toDate()
添加到了moment.js
日期对象的末尾,现在一切都按预期工作了!
现在,我的过滤如下所示:
onMonthRangeChange: function (e) {
var value = e.sender.value();
switch (value) {
case "1":
var oneMonth = moment().add(1, "month").toDate();
var firstOfMonth = moment().startOf("month").toDate();
viewModel.myTaskDataSource.filter({
logic: "and",
filters: [
{ field: "DueDate", operator: "gte", value: firstOfMonth },
{ field: "DueDate", operator: "lte", value: oneMonth }
]
});
break;
case "3":
// similar to above code except for 3 months
case "6":
// similar to above code except for 6 months
case "9":
// similar to above code except for 9 months
case "12":
// similar to above code except for 12 month
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.