感谢Sandman为我提供一个链接到他的问题,这是非常类似地雷,而他能够找到一个解决方案。我已经改变了他的解决方案,以便它对我有用。 Original Solution。
function applyClientFilters() {
var listView = $("#ProductsList").data("kendoListView");
var listViewDataSource = listView.dataSource;
// clear existing datasource
listViewDataSource.filter([]);
// extract selected items from each of the dropdown controls and the search box
var productType = $("#ProductTypeDropDown").data("kendoDropDownList").value();
var therapeuticClass = $("#TherapeuticClassDropDown").data("kendoDropDownList").value();
var searchString = $(".search-filter").val();
// setup filter object (using and logic)
var filter = {
logic: "and",
filters: [] // ready for filter from each of the dropdowns and search bar
};
// push new filter into array of filters with or logic on each filter
if (productType.length > 0) {
var productTypeFilter = {
logic: "or",
filters: [
{ field: "ProductTypeId", operator: "equals", value: productType }
]
};
filter.filters.push(productTypeFilter);
}
if (therapeuticClass.length > 0) {
var therapeuticClassFilter = {
logic: "or",
filters: [
{
field: "TherapeuticClasses",
operator: function (itemValue, value) {
return itemValue &&
itemValue.find(function (item) {
if (item.Name.toLowerCase().indexOf(value.toLowerCase()) !== -1) {
return true;
} else {
return false;
}
});
},
value: therapeuticClass
}
]
};
filter.filters.push(therapeuticClassFilter);
}
if (searchString.length > 0) {
var searchFilter = {
logic: "or",
filters: [
{ field: "Name", operator: "startswith", value: searchString },
{ field: "ProductTypeDisplay", operator: "startswith", value: searchString },
{ field: "NdcCode", operator: "startswith", value: searchString },
{
field: "TherapeuticClasses",
operator: function(itemValue, value) {
return itemValue &&
itemValue.find(function(item) {
// If a therapeutic class name matches search then return true
if (item.Name.toLowerCase().indexOf(value.toLowerCase()) !== -1) {
return true;
} else {
return false;
}
});
},
value: searchString
}
]
};
filter.filters.push(searchFilter);
}
// apply filter query to datasource
listViewDataSource.query({
filter: filter
});
}
然后我打电话applyClientFilters()
功能在每个下拉菜单和搜索框的变化情况和过滤的伟大工程。
我想筛选使用多个多选择下拉菜单网格和管理使用的答案来解决的时候碰到了类似的问题。 [检查出来](https://stackoverflow.com/questions/43780495/custom-filtering-with-multi-select-drop-downs)看看是否有帮助。 :) – Sandman
当您使用退格键和其他键删除过滤器时,您的文本框不会触发过滤器。我无法完整地查看代码,但是,我建议您将逻辑压缩为一个过滤器方法,并传入网格或数据源,并让所有更改事件处理程序调用一个过滤器函数,以检查所有字段并构建过滤器并将其应用于数据源。 –
我给你读了最后一段,并按照你的指示。您正在处理所有事件,但是,您的产品下拉事件不会查找名称上是否存在过滤器。您必须在一个功能中同时处理两个或全部三个过滤器,才能按照您的喜好进行工作。 –