Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear the filtering with out clicking clear button

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

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");
like image 483
user2138545 Avatar asked Mar 13 '13 06:03

user2138545


People also ask

What is the shortcut to clear filters?

Remove all the filters in a worksheet If you want to completely remove filters, go to the Data tab and click the Filter button, or use the keyboard shortcut Alt+D+F+F.

How do you clear a sorting filter?

Go to Home tab > Sort & Filter > Clear to clear the sorting/filtering. This will remove all filters to sort state and get rid of sort arrows. However, this method will not restore the data table to its original state/ initial sort order. See the following explanation.

Why is my filter stuck in Excel?

Check that a filter hasn't been left on another column. The best way to clear all of the filters is to click the Clear button on the Ribbon (to the right of the Filter button). This then leaves Filter turned on, but removes all filter settings allowing you to start again with the full set of your data.

How do I unlock a filter in Excel?

On the Home tab, click the Format Cell Font popup launcher. You can also press Ctrl+Shift+F or Ctrl+1. In the Format Cells popup, in the Protection tab, uncheck the Locked box and then click OK.


2 Answers

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

$("#grid").data("kendoGrid").dataSource.filter([]);
like image 166
Atanas Korchev Avatar answered Oct 09 '22 11:10

Atanas Korchev


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:

{
    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:

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

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

like image 6
KubaKubikula Avatar answered Oct 09 '22 11:10

KubaKubikula