如何根据列过滤器和搜索框文本值来管理Kendo网格

时间:2017-05-31 10:20:27

标签: kendo-ui kendo-grid

我有一个Kendo网格,我已经应用了一个列过滤器,我需要使用我试过的文本框值过滤此结果

var sText = $("#searchBox").val();
                if (sText.length > 0) {
                var filterChild = [];
                var filtOut = [];
                var ds = $('#homeGridCustomer').data('kendoGrid').dataSource;
                var curr_filters = ds.filter();
                filterChild.push({ field: "Name", operator: "contains", value: sText });
                filterChild.push({ field: "City", operator: "contains", value: sText });
                filterChild.push({ field: "CustomerNo", operator: "contains", value: sText });
                var filtParent = { filters: filterChild, logic: "or" };
                if (curr_filters != undefined) {
                    filtOut.push(curr_filters);
                }
                filtOut.push(filtParent);
                ds.filter({ filters: filtOut, logic: "and" });

但它没有获得过滤后的输出,没有显示任何内容。有人可以帮忙吗?提前致谢

1 个答案:

答案 0 :(得分:1)

如果您希望根据相同的输入值将多个搜索过滤器应用于dataSource中的多个列,并维护已应用的现有过滤器(可以在搜索区域中未输入任何内容时清除过滤器),尝试使用:

var ds = $("#grid").data("kendoGrid").dataSource;        

var sText = $("#nameOfYourInput").val();
var filterToApply = ds.filter();
if(!filterToApply) {
  // if not, setup new filter
  filterToApply = {
     logic: "and", // use OR here if you want to search for a value in either column instead of all columns
     filters: []
  }
}

var applyFilters = false;
// check to see if `sText` contains value before filtering (as filter will apply like `Name`/`City`/`CustomerNo` `contains` `null`/`undefined` otherwise)
if(sText) {
  applyFilters = true;
  filterToApply.filters.push({field: "foo", operator: "contains", value: sText});
  filterToApply.filters.push({field: "bar", operator: "contains", value: sText});    
}
if(applyFilters) {
  ds.filter([filterToApply]);
}
else {
  ds.filter([]);
}

我附加了一个jsbin example供您使用,默认根据新输入值覆盖现有网格过滤器,并包含已注释掉的代码,可用于在现有过滤器之上构建。希望你能从中得到你需要的东西。