单击清除按钮清除过滤

时间:2013-03-13 06:29:46

标签: javascript kendo-ui

我的应用程序中有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");

2 个答案:

答案 0 :(得分:14)

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

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

答案 1 :(得分:5)

如果你打电话

grid.dataSource.filter({})

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

我开发了一种方法,它只删除网格过滤器。

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);
}

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

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;
}

可以像这样调用方法:

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

递归就在那里因为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"
        }
    ],
}

和方法在所有嵌套的'过滤器'数组上递归调用。 上面的过滤器就像:

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

该方法并不完美且经过一些测试。 我希望这有助于某人。