简体   繁体   English

Kendo Grid具有多选列的自定义过滤器功能

[英]Custom filter function for Kendo Grid with multiselect column

I need to write a custom filter function. 我需要编写一个自定义过滤器函数。 I have a kendo grid with multiple columns, ie name, age, city. 我有一个Kendo网格,其中有多个列,即名称,年龄,城市。 The name column should be multiselect. 名称列应为多选。 Now, when filtering, the logic on the entire grid should be "and" but that specific column (name) should have an "or" logic. 现在,当进行过滤时,整个网格上的逻辑应为“和”,但特定的列(名称)应具有“或”逻辑。

I have seen this post and several other similar posts. 我看过这篇文章和其他几篇类似的文章。 In this example, telerik suggest removing the data-bind attribute 在此示例中,telerik建议删除data-bind属性

element.removeAttr("data-bind");

This works really well, until I want to clear all of the tags from the multiselect field. 直到我要清除multiselect字段中的所有标签之前,这确实非常有效。 FYI, the grid is configured as filterMode: "row". 仅供参考,将网格配置为filterMode:“行”。 In this scenario, the tags do not clear at all. 在这种情况下,标记根本不会清除。

So now, here I am attempting to write a custom filter function. 所以现在,我在这里尝试编写自定义过滤器函数。 Here is what I have so far: 这是我到目前为止的内容:

filterable: {
    multi: true,
    cell: {
        template: function getteamplate(args) {
            args.element.kendoMultiSelect({
                dataSource: args.dataSource,
                dataTextField: "name",
                dataValueField: "name",
                change: function change(e) {
                    var dataSource = $scope.grid.dataSource;

                    // if filters are not yet set, do so now
                    if (!dataSource.filter()) {
                         $scope.grid.dataSource.filter({
                             logic: "and",
                             filters: []
                         });
                    }

                    var dataFilters = dataSource.filter().filters;

                    var values = this.value();
                    if (values.length > 0) {
                        $log.log("filtering");
                        var newFilter = {
                            field: "name",
                            operator: function operator(item, value) {
                                $log.log("Item: " + item);
                                $log.log(value());
                                var found = false;

                                value().forEach(function forEach(element) {
                                    $log.log("Value: " + element);
                                    if (item.indexOf(element) !== -1) {
                                       found = true;
                                    }
                                });
                                return found;
                            },
                            value: values,
                            fieldName: "dataSource"
                        };

                        dataFilters.push(newFilter);
                        $log.log(dataFilters);
                    }

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

            //args.element.removeAttr("data-bind");
        },

        showOperators: false
    }
}

A couple of things worth noting 有两点值得注意

  1. My custom filter function never seems to execute, as I do not see anything in the console. 我的自定义过滤器功能似乎从未执行,因为在控制台中看不到任何内容。
  2. Following this forum post , this code seems very straight forward: 在此论坛帖子之后 ,此代码似乎非常简单:

    operator: function(item, value){ //implement your logic } 运算符:function(item,value){//实现逻辑}

Except it's not documented well and I'm not sure what the parameters 'item' and 'value' are or where they come from. 除非记录得不好,而且我不确定参数“ item”和“ value”是什么或它们来自何处。

A little bit late but nevermind. 有点迟了,但是没关系。 In order the tags to be cleared properly i added the following things 为了正确清除标签,我添加了以下内容

  1. Added an id property to the dom element of the multiselect object as follows 向multiselect对象的dom元素添加了一个id属性,如下所示

  function getteamplate(args) { args.element[0].id = "<<filterElementID>>"; args.element.kendoMultiSelect({ dataSource: args.dataSource, ............. 

  1. Added a check in the grid's dataSource dataBound event such that whenever the dataSource filters does not contain the "MultiSelect" filter (filter.field = name) clear the dom element's value 在网格的dataSource dataBound事件中添加了一个检查,以便每当dataSource过滤器不包含“ MultiSelect”过滤器(filter.field = name)时,清除dom元素的值

 if(!_.find(this.dataSource.filter() ? this.dataSource.filter().filters : [] , function(filter){ return filter.field == "name" })) $("#<<filterElementID>>").data().kendoMultiSelect.value([]); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM