我有一个带有工具栏模板的Telerik Kendo Grid,用于创建个性化过滤器。
我的工具栏中有2个下拉列表,值选择必须在"和"使用所有过滤器(包括网格的默认过滤器)。
当我想删除过滤器时,我使用此功能:
CODE
<script type="text/javascript">
function removeFilter(filter, searchFor) {
if (filter == null)
return [];
for (var x = 0; x < filter.length; x++) {
if (filter[x].filters != null && filter[x].filters.length >= 0) {
if (filter[x].filters.length == 0) {
filter.splice(x, 1);
return removeFilter(filter, searchFor);
}
filter[x].filters = removeFilter(filter[x].filters, searchFor);
}
else {
if (filter[x].field == searchFor) {
filter.splice(x, 1);
return removeFilter(filter, searchFor);
}
}
}
return filter;
}
</script>
我的问题是我的函数removeFilter删除了gridview中的所有过滤器,而我只删除了特定的字段。
我在jsfiddle中重现Example。
问题
从网格过滤器中仅删除特定字段的正确方法是什么?
参考
答案 0 :(得分:0)
你的错误在myFilterChange function
。您在更改时添加过滤器,但从不删除上一个过滤器。在此结果中,在第二次更改此组合框后,您的条件如idcat == 1 && idcat == 2
。
您应该在为同一列fiddle添加另一个列之前清除过滤器:
if (statoFilterValue) {
removeFilter(currFilters.filters, "idstate");
currFilters.filters.push({ field: "idstate", operator: "eq", value: parseInt(statoFilterValue) });
} else {
removeFilter(currFilters.filters, "idstate");
}
同样在removeFilter function
中,您不应该使用递归与拼接相结合。足以确定splice
if (filter[x].filters.length == 0) {
console.log('empty');
filter = filter.splice(x, 1);
}
答案 1 :(得分:0)
按文件名删除过滤器
function removefilter(filterField)
{
//grid id
var gridData = $("#your grid id").data("kendoGrid");
// get currently applied filters from the Grid.
var currFilterObj = gridData.dataSource.filter();
// get current set of filters, which is supposed to be array.
// if the oject we obtained above is null/undefined, set this to an empty array
var currentFilters = currFilterObj ? currFilterObj.filters : [];
// iterate over current filters array. if a filter for "filterField" is already
// remove filter by filed name
if (currentFilters && currentFilters.length > 0) {
for (var i = 0; i < currentFilters.length; i++) {
// for(var j=0; j<)
if (currentFilters[i].field == filterField) {
currentFilters.splice(i, 1);
break;
}
}
}
gridData.dataSource.filter({
logic: "and",
filters: currentFilters
});
}
添加过滤器
function applyFilter(filterField, filterValue) {
// your grid id
var gridData = $("#grid id").data("kendoGrid");
// get currently applied filters from the Grid.
var currFilterObj = gridData.dataSource.filter();
// get current set of filters, which is supposed to be array.
// if the oject we obtained above is null/undefined, set this to an empty array
var currentFilters = currFilterObj ? currFilterObj.filters : [];
// iterate over current filters array. if a filter for "filterField" is already
// defined, remove it from the array
// once an entry is removed, we stop looking at the rest of the array.
if (currentFilters && currentFilters.length > 0) {
for (var i = 0; i < currentFilters.length; i++) {
if (currentFilters[i].field == filterField) {
currentFilters.splice(i, 1);
break;
}
}
}
// if "filterValue" is "0", meaning "-- select --" option is selected, we don't
// do any further processing. That will be equivalent of removing the filter.
// if a filterValue is selected, we add a new object to the currentFilters array.
if (filterValue != "0") {
currentFilters.push({
field: filterField,
operator: "eq",
value: filterValue
});
}
debugger;
// finally, the currentFilters array is applied back to the Grid, using "and" logic.
gridData.dataSource.filter({
logic: "and",
filters: currentFilters
});
}