Get all rows not filtered from jqGrid

There are no direct way to get the information which you need. Internally jqGrid uses $.jgrid.from to filter local data. The main code which uses $.jgrid.from in inside of addLocalData. To get results which you need without studying all the code I suggest to use the fact that all filtered data will be returned by select method of $.jgrid.from (see the line of code). My suggestion is to catch the data before the data will be cut to the page size.

To do this I suggest to use sub-classing: overwriting of the method select method of $.jgrid.from. I demonstrate the technique in the examples created for the answer and this one.

In your case the code will be

var oldFrom = $.jgrid.from,
    lastSelected;

$.jgrid.from = function (source, initalQuery) {
    var result = oldFrom.call(this, source, initalQuery),
        old_select = result.select;
    result.select = function (f) {
        lastSelected = old_select.call(this, f);
        return lastSelected;
    };
    return result;
};

Now the variable lastSelected will save the array of elements which are results of the last sorting or filtering operation. Because $.jgrid.from is global the data are not connected to the grid. If you have more as one grid on the page it will be uncomfortable. One can fix the small disadvantage with the following line in the code of loadComplate of every grid:

loadComplete: function () {
    this.p.lastSelected = lastSelected; // set this.p.lastSelected
}

In the way we introduce new jqGrid parameter lastSelected which will have close structure as data parameter, but will hold only last filtered data.

The following code will display the ids of filtered data in alert message

$("#getIds").click(function () {
    var filteredData = $grid.jqGrid('getGridParam', 'lastSelected'), i, n, ids = [],
        idName = $grid.jqGrid('getGridParam', 'localReader').id;
    if (filteredData) {
        for (i = 0, n = filteredData.length; i < n; i++) {
            ids.push(filteredData[i][idName]);
        }
        alert("tolal number of filtered data: " + n + "\n" +
            "ids of filtered data:\n" + ids.join(', '));
    }
});

I used localReader.id parameter because property name used for local data are typically id or _id_. The _id_ will be used in case of data loaded from the server if one uses loadonce: true option.

The demo demonstrate the approach. If one filter for example only the data from FedEx and then clicks on “Show Ids” button one will see information about all filtered and not only about the data displayed on the current page:

enter image description here

enter image description here

UPDATED: free jqGrid provides new lastSelectedData option. See the demo in the list of demos.

Leave a Comment