[英]Kendo UI Grid - Add/Remove filters dynamically
I need to Create a Kendo ui grid.我需要创建一个 Kendo ui 网格。 Since this has many filters, I need to have 4 regular filters and rest should be able to add dynamically according to users choice.由于这有很多过滤器,我需要有 4 个常规过滤器,rest 应该能够根据用户选择动态添加。 Can someone provide assistance on this?有人可以提供帮助吗?
In order to filter by text box you can hook up a keyUp
event in order to retrieve the value.为了按文本框过滤,您可以连接一个keyUp
事件以检索值。 You can then add this as a filter to the existing filter
object.然后,您可以将其作为过滤器添加到现有filter
object。
$('#NameOfInput').keyup(function () {
var val = $('#NameOfInput').val();
var grid = $("#yourGrid").data("kendoGrid");
var filter = grid.dataSource.filter();
filter.filters.push({
field: "NameOfFieldYouWishToFilter",
operator: "eq",
value: val,
FilterName: "UniqueIdentifierForFilter"
});
grid.dataSource.filter(filter);
});
Using a drop down box, you can achieve the desired functionality by using the onChange
event, get the value using $('#yourDropDown').val();
使用下拉框,您可以通过使用onChange
事件实现所需的功能,使用$('#yourDropDown').val();
. .
The FilterName
is optional incase you require additional logic to add/remove filters. FilterName
是可选的,以防您需要额外的逻辑来添加/删除过滤器。 ie you can use this to determine whether the filter already exists in the array and if so you can use splice
to remove it.也就是说,您可以使用它来确定过滤器是否已存在于数组中,如果存在,您可以使用splice
将其删除。
EDIT编辑
Using FilterName
you can search to see if a filter already exists and remove it:使用FilterName
您可以搜索以查看过滤器是否已存在并将其删除:
var filterIndex = filter.filters.map((e: any) => { return e.FilterName }).indexOf("UniqueIdentifierForFilter");
if (filterIndex > -1)
{
filter.filters.splice(filterIndex, 1);
}
For @lakshan, while this is largely correct, you will get an error if there are no filters at first.对于@lakshan,虽然这在很大程度上是正确的,但如果一开始没有过滤器,您将收到错误消息。 I found this answer when I encountered the undefined filter error.当我遇到未定义的过滤器错误时,我找到了这个答案。 My full solution for adding a filter, either to an undefined filter set, or along with an existing one:我将过滤器添加到未定义的过滤器集或与现有过滤器一起添加的完整解决方案:
var grid = $("#ActivityGrid").data("kendoGrid");
var dataSource = grid.dataSource;
var gridFilter = dataSource.filter();
var upcomingFilter = {
field: "ActivityDate",
operator: "gte",
value: new Date(),
FilterName: "UpcomingOnly"
};
if ($("#UpcomingOnlyCheckbox")[0].checked) {
if (gridFilter == undefined) {
dataSource.filter(upcomingFilter);
}
else {
gridFilter.filters.push(upcomingFilter);
dataSource.filter(gridFilter);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.