Completed
Last Updated: 22 Oct 2021 12:46 by ADMIN
Imported User
Created on: 02 Apr 2014 14:45
Category: Grid
Type: Feature Request
2
Retrieve filtered set from DataSource or Grid
In lieu or as part of select-all capability:

Expose a way to retrieve the filtered set of data (or keys) in the grid and other filterable controls.

In other words, when a user has applied their own filters, there is currently no method to retrieve the filtered set across the entire dataset. That makes implementing a select-all button a bit tricky as you have to either select all on the current page (view) or the entire unfiltered dataset and then apply the filter yourself.
3 comments
ADMIN
Telerik Admin
Posted on: 25 Apr 2014 15:23
Is the solution posted by Sebastian good enough? It looks pretty straight-forward and requires a few lines of code.
ADMIN
Sebastian
Posted on: 22 Apr 2014 11:55
Here is another workaround that can be used to get the filtered data.

var dataSource = $("#grid").data("kendoGrid").dataSource;
var filters = dataSource.filter();
var allData = dataSource.data();
var query = new kendo.data.Query(allData);
var data = query.filter(filters).data;
Imported User
Posted on: 02 Apr 2014 18:24
In case anyone needs a workaround, here's a first draft of what I've come up with. You'll need to add additional tests in the switch-case statement.

Where rawData is the data() from dataSource, and filter is the filter():

        var filteredData = $.grep(rawData, function(elementOfArray, indexInArray){
            return filterData(elementOfArray, indexInArray, filter);
        });

function filterData(elementOfArray, indexInArray, filter) {
    var returnValue = true;

    if (filter.logic == 'or') {
        returnValue = false;
    }

    for (var i = 0; i < filter.filters.length; i++) {
        if (filter.filters[i].filters) {
            returnValue = filterData(elementOfArray, indexInArray, filter.filters[i]);
        }
        else {
            switch (filter.filters[i].operator) {
                //be sure to cover every operator specified or inferred in the grid definition
                case 'startswith':
                    returnValue = eval("elementOfArray." + filter.filters[i].field + ".toLowerCase().indexOf(filter.filters[i].value.toLowerCase()) == 0");
                    break;
                case 'contains':
                    returnValue = eval("elementOfArray." + filter.filters[i].field + ".toLowerCase().indexOf(filter.filters[i].value.toLowerCase()) > -1");
                    break;
                default:
                    //test if it's a (custom) function
                    try {
                        returnValue = filter.filters[i].operator();
                    }
                    catch (ex) {
                        returnValue = false;
                    }
                    break;
            }
        }

        if (filter.logic == 'or' && returnValue) {
            break;
        }
        else if (filter.logic == 'and' && !returnValue) {
            break;
        }
    }

    return returnValue;
}