xszaboj
xszaboj

Reputation: 1085

Kendo UI clear filter by field

I have kendo grid on my page. This grid has datasource. And in this datasource I have array of complex filters. For example:

http://s7.postimg.org/bmqxgp2ff/filters.png

And before I add new set of filters to my datasource filters I need to remove all filters where field= "fld_32" for example.

I think it is possible to do this by recursion, but on grid there is a filter component that has button "clear" and button clear does the same thing that I need. But I have no idea how it works and how to call it programatically.

Upvotes: 3

Views: 5127

Answers (2)

KubaKubikula
KubaKubikula

Reputation: 370

I extend kendoGrid by method for erase all filters in grid:

kendo.ui.Grid.prototype.clearFilters = function(args){
// MODIFY THIS PART
    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)
        }
    }
}
// MODIFY THIS PART END

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

For your issue you can modify clearFilters method to take in argument the name of the field to erase from filter ("fld_32"). Then this field can be inserted to _eraseFiltersField parameter instead fileds of grid.

Upvotes: 3

U A
U A

Reputation: 139

All you need to do is use this function. This will clear all the filters:

function clearFiter() {
    $("form.k-filter-menu button[type='reset']").trigger("click");
}

Upvotes: 0

Related Questions