如何在 Kendo 网格中使用自定义过滤保留以前的过滤值?

How to keep previous filter values with custom filtering in Kendo Grid?

我创建了自定义筛选,因为有一个列,其中 (1) 显示对象数组中的某个 属性,并且 (2) 可以显示两种类型的对象中的任何一种该列的每一行:declarantsdeclarantMeetings.

以下代码用于过滤。问题是,如果用户按其他列过滤了网格,则此过滤器不会添加到以前的过滤器中 - 所有以前的过滤器都被删除。因此无法按多列进行筛选。

如何保留旧过滤器?

var getGridOptions = function (dataSource) {
    return {
        dataSource: dataSource,
        //...
        filter: function (e) {
            var field = e.filter.filters[0].field;

            //if the user is filtering the column with multiple possible object arrays
            if (field == "declarants") {
                e.preventDefault();

                var filters = [];
                var filterValue = e.filter.filters[0].value.toLowerCase();

                var _personFilter = {
                    operator: function (item, filterValue) {
                        var properties = ["firstName", "lastName", "otherNames", "position", "email", "fullNameAndDivision"];
                        if (typeof item !== "undefined") {
                            for (var k = 0; k < item.length; k++) {
                                for (var i = 0; i < properties.length; i++) {
                                    if (typeof item[k][properties[i]] !== "undefined") {
                                        var value = item[k][properties[i]].toLowerCase();
                                        if (value.indexOf(filterValue) !== -1) {
                                            return true;
                                        }
                                    }
                                }
                            }
                        }
                    },
                    value: filterValue,
                    field: field
                };
            
                var subfilters = [];
                subfilters.push({
                    operator: function (item, filterValue) {
                        if (typeof item !== "undefined") {
                            for (var k = 0; k < item.length; k++) {
                                if (typeof item[k]["meetingType"]["name"] !== "undefined") {
                                    var value = item[k]["meetingType"]["name"].toLowerCase();
                                    if (value.indexOf(filterValue) !== -1) {
                                        return true;
                                    }
                                }
                            }
                        }
                    },
                    value: filterValue,
                    field: "declarantMeetings"
                });
                subfilters.push(_personFilter);
                filters.push({logic: "or", filters:subfilters})

                e.sender.dataSource.filter({
                    logic: "and",
                    filters: filters
                });
            }
        }
    }
}

您可以使用 dataSource.filter 方法获取所有现有过滤器。因此,您可以使用自定义过滤器扩展现有过滤器查询,如下所示:

function removeFiltersForField(expression, field) {
    if (expression.filters) {
        expression.filters = $.grep(expression.filters, function(filter) {
            removeFiltersForField(filter, field);
            if (filter.filters) {
                return filter.filters.length;
            } else {
                return filter.field != field;
            }
        });
    }
}

var getGridOptions = function (dataSource) {
    return {
        dataSource: dataSource,
        //...
        filter: function (e) {
            var field = e.filter.filters[0].field;

            //if the user is filtering the column with multiple possible object arrays
            if (field == "declarants") {
                e.preventDefault();

                var filters = e.sender.dataSource.filter() || { filters:[], logic: "and" };
                var filterValue = e.filter.filters[0].value.toLowerCase();

                removeFiltersForField(field, filters);

                var _personFilter = {
                    operator: function (item, filterValue) {
                        var properties = ["firstName", "lastName", "otherNames", "position", "email", "fullNameAndDivision"];
                        if (typeof item !== "undefined") {
                            for (var k = 0; k < item.length; k++) {
                                for (var i = 0; i < properties.length; i++) {
                                    if (typeof item[k][properties[i]] !== "undefined") {
                                        var value = item[k][properties[i]].toLowerCase();
                                        if (value.indexOf(filterValue) !== -1) {
                                            return true;
                                        }
                                    }
                                }
                            }
                        }
                    },
                    value: filterValue,
                    field: field
                };
            
                var subfilters = [];
                subfilters.push({
                    operator: function (item, filterValue) {
                        if (typeof item !== "undefined") {
                            for (var k = 0; k < item.length; k++) {
                                if (typeof item[k]["meetingType"]["name"] !== "undefined") {
                                    var value = item[k]["meetingType"]["name"].toLowerCase();
                                    if (value.indexOf(filterValue) !== -1) {
                                        return true;
                                    }
                                }
                            }
                        }
                    },
                    value: filterValue,
                    field: "declarantMeetings"
                });
                subfilters.push(_personFilter);
                filters.filters.push({logic: "or", filters:subfilters});

                e.sender.dataSource.filter(filters);
            }
        }
    }
}