Remove Filters from data source

7 posts, 0 answers
  1. Greg Lavelle
    Greg Lavelle avatar
    3 posts
    Member since:
    Oct 2006

    Posted 24 May 2012 Link to this post

    Is it possible to remove a specified filter from he datasource in javascript.  I would like to dynamically add/remove a filter after checking/unchecking a checkbox.

    for example:

    var filters = datasource.filter();
    for(i=0;i<filters.length;i++){
        ...
        [conditional code here]
        ....
        filters[i].splice(i,1);
    }



  2. Eric
    Eric avatar
    12 posts
    Member since:
    Aug 2010

    Posted 14 Jun 2012 Link to this post

    I am looking to do the exact same thing.  Did you find a solution?
  3. Kendo UI is VS 2017 Ready
  4. Eric
    Eric avatar
    12 posts
    Member since:
    Aug 2010

    Posted 14 Jun 2012 Link to this post

    I found a solution.  You have to modify the filter object to remove your filter, then set the filter to the modified filter object:

            var datasource = $("#Grid").data("kendoGrid").dataSource;
            var filters = datasource.filter();

    Clear filters:
                    datasource.filter([]);
    
     Remove a filter:
    
                            filters.filters.splice(i, 1);
                            datasource.filter(filters);
    Add a filter:
    filters.filters.push({ field: "Filter", operator: "eq", value: 1 });
    
    datasource.filter(filters);
    When you get to the details, its harder than it looks to modify the filter object.
  5. James Hood
    James Hood avatar
    27 posts
    Member since:
    Jun 2012

    Posted 19 Jun 2013 Link to this post

    thread necro..

    But this helped me, although instead of building a filter array, I have a sort method on a datasource, which is being called from a function and a switch

    to remove the filter (IE reset it back to initial view) all I did was datasource.filter({})

    switch (style) {
        case 1:
            FoodMenuDataSource.filter({ field: "Level", operator: "eq", value: 1 });
            break;
        case 2:
            FoodMenuDataSource.filter({ field: "Level", operator: "eq", value: 2 });
            break;
        case 3:
            FoodMenuDataSource.filter({ field: "Level", operator: "eq", value: 3 });
            break;
        case 4:
            FoodMenuDataSource.filter({});
            break;
    }
  6. Will
    Will avatar
    34 posts
    Member since:
    Oct 2013

    Posted 23 Jan 2014 Link to this post

    @James Hood - Excellent!  Thanks for sharing this.
  7. Jeff
    Jeff avatar
    1 posts
    Member since:
    Mar 2014

    Posted 24 Apr 2014 Link to this post

    I know this thread is rather old, but here is our solution which works quite well even for nested filters.

    function removeFilter(filter, searchFor) {
        if (filter == null)
            return [];
     
        for (var x = 0; x < filter.length; x++) {
     
            if (filter[x].filters != null && filter[x].filters.length >= 0) {
                if (filter[x].filters.length == 0) {
                    filter.splice(x, 1);
                    return removeFilter(filter, searchFor);
                }
                filter[x].filters = removeFilter(filter[x].filters, searchFor);
            }
            else {
                if (filter[x].field == searchFor) {
                    filter.splice(x, 1);
                    return removeFilter(filter, searchFor);
                }
            }
        }
         
        return filter;
    }

    To use this you call it with the filters and field name (searchFor) you want to remove. 
    Ex: 
    filters = $('#divGrid').data('kendoGrid').dataSource.filter().filters;
     
    //Remove our old filters
    filters = removeFilter(filters, 'orgId');
    filters = removeFilter(filters, 'cliID');
  8. Hunter
    Hunter avatar
    24 posts
    Member since:
    Jan 2014

    Posted 27 Aug 2014 in reply to Jeff Link to this post

    Function does not work on this filter configuration, when I attempt to remove all "status" related entries:

    {
        "logic": "and",
        "filters": [
            {
                "filters": [
                    {
                        "field": "name",
                        "operator": "contains",
                        "value": "JOHNSON"
                    },
                    {
                        "field": "city",
                        "operator": "contains",
                        "value": "MILWAUKEE"
                    }
                ],
                "logic": "and"
            },
            {
                "logic": "or",
                "filters": [
                    {
                        "field": "status",
                        "operator": "eq",
                        "value": "G"
                    },
                    {
                        "field": "status",
                        "operator": "eq",
                        "value": "H"
                    },
                    {
                        "field": "status",
                        "operator": "eq",
                        "value": "I"
                    }
                ]
            }
        ]
    }
Back to Top
Kendo UI is VS 2017 Ready