javascript 无需单击清除按钮即可清除过滤

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15378470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:35:33  来源:igfitidea点击:

Clear the filtering with out clicking clear button

javascriptkendo-ui

提问by user2138545

I have kendo-grid in my application.And its have filterable "true".When we apply the filtering then grid items are filtered and grid size also re-sized. when we clear the text in filter column then automatically grid display the items which is displayed in the page-load with out pressing clear button.is it possible? My grid code is

我的应用程序中有 kendo-grid。它具有可过滤的“true”。当我们应用过滤时,网格项目会被过滤,并且网格大小也会重新调整大小。当我们清除过滤器列中的文本然后自动网格显示页面加载中显示的项目而不按清除按钮。这可能吗?我的网格代码是

var grid = $("#grid").kendoGrid({
  dataSource: {
    type  : "odata",
    transport      : {
      read: "http://demos.kendoui.com/service/Northwind.svc/Orders"
    },
    schema         : {
      model: {
        fields: {
          OrderID  : { type: "number" },
          Freight  : { type: "number" },
          ShipName : { type: "string" },
          OrderDate: { type: "date" },
          ShipCity : { type: "string" }
        }
      }
    },
    pageSize       : 10
  },
  filterable: true,
  sortable  : true,
  pageable  : true,
  columns   : [
    {
    field     : "OrderID",
    filterable: false
  },
  "Freight",
  {
    field : "OrderDate",
    title : "Order Date",
    width : 100,
    format: "{0:MM/dd/yyyy}"
  },
  {
    field: "ShipName",
    title: "Ship Name",
    width: 200
  },
  {
    field: "ShipCity",
    title: "Ship City"
  }
  ]
}).data("kendoGrid");

回答by Atanas Korchev

You need to use the filtermethod of the grid's data source:

您需要使用网格的数据源的过滤方法:

$("#grid").data("kendoGrid").dataSource.filter([]);

回答by KubaKubikula

if you call

如果你打电话

grid.dataSource.filter({})

there is a possibility, that you erase whole dataSource filter, not only fields which are in grid. I mean dataSource can be prefiltered for some reason.

有可能您删除整个数据源过滤器,而不仅仅是网格中的字段。我的意思是可以出于某种原因对数据源进行预过滤。

I developed method, which remove only filter of grid.

我开发了只删除网格过滤器的方法。

kendo.ui.Grid.prototype.clearFilters = function(args){
    var ignore = [];
    // test arguments
    if(typeof args === 'object'){
        if(args.hasOwnProperty('ignore')){
            if(args.ignore.length > 0){
                ignore = args.ignore;
            }
        }
    }

    // get dataSource of grid and columns of grid
    var fields = [], filter = this.dataSource.filter(), col = this.columns;
    if( $.isEmptyObject(filter) || $.isEmptyObject(filter)) return;

    // Create array of Fields to remove from filter and apply ignore fields if exist
    for(var i = 0, l = col.length; i < l; i++){
        if(col[i].hasOwnProperty('field')){
            if(ignore.indexOf(col[i].field) === -1){
                fields.push(col[i].field)
            }
        }
    }

    if($.isEmptyObject(fields)) return;

    // call "private" method
    var newFilter = this._eraseFiltersField(fields, filter)

    // set new filter
    this.dataSource.filter(newFilter);
}

And here is second method. It is separated because it can be call recursively:

这是第二种方法。它是分开的,因为它可以递归调用:

kendo.ui.Grid.prototype._eraseFiltersField = function(fields, filter){
    for (var i = 0; i < filter.filters.length; i++) {

        // For combination 'and' and 'or', kendo use nested filters so here is recursion
        if(filter.filters[i].hasOwnProperty('filters')){
            filter.filters[i] = this._eraseFiltersField(fields, filter.filters[i]);
            if($.isEmptyObject(filter.filters[i])){
                filter.filters.splice(i, 1);
                i--;
                continue;
            }
        }

        // Remove filters
        if(filter.filters[i].hasOwnProperty('field')){
            if( fields.indexOf(filter.filters[i].field) > -1){
                filter.filters.splice(i, 1);
                i--;
                continue;
            }
        }
    }

    if(filter.filters.length === 0){
        filter = {};
    }

    return filter;
}

Method can be called like this:

方法可以这样调用:

$('#my-grid').data('kendoGrid').clearFilters({
    ignore: ['Field1', 'Field2']
})

Recursion is there because dataSource filter can look like:

存在递归是因为 dataSource 过滤器看起来像:

{
    logic: "and"
    filters: [
        {
            logic: "or"     
            filters:[
                        {
                            field: "Field1"
                            operator: "contains"
                            value: "val1"
                        },
                        {
                            field: "Field1"
                            operator: "contains"
                            value: "val2"
                        }
            ],
        },
        {
            field: "Field3"
            operator: "contains"
            value: "val3"
        }
    ],
}

and method is recursively called on all nested 'filters' arrays. Filter above is like:

and 方法在所有嵌套的“过滤器”数组上递归调用。上面的过滤器是这样的:

("Field3" === "val3" && ("Field1" === "val1" || "Field1" === "val2" ) )

The method is not perfect and a few tested. I hope this helps someone.

该方法并不完美,并进行了一些测试。我希望这可以帮助别人。