Get all rows not filtered from jqGrid
Asked Answered
A

4

6

I have local data in a grid. How can I get all of the rows or IDs that are not removed after a user uses the filter toolbar? I need to get all filtered rows, regardless of pagination.

For example, say I begin with 50 rows in the grid. The user uses the filter toolbar and the set of rows decreases to 10 rows. How can I get those ten rows?

Artemus answered 19/3, 2012 at 17:43 Comment(0)
B
24

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.

Banana answered 22/3, 2012 at 21:52 Comment(7)
Thank you for your well-described answers as always. I am trying your solution now.Artemus
@JackB.: I am not sure that you know this. If you plan to award a bounty you should do this explicitly by clicking on the "+XX" (see here for more details)Banana
You are my hero! You've answered every one of my jqGrid questions before I even had a chance to ask you :)Mulry
@monitorjbl: I'm glad that I could help you. You are welcome!Banana
I would be lost without you Oleg! Thank you so much for all your contributions to the community.Britneybritni
@Migs: You are welcome! I'm glad to know that I could help you too.Banana
@Banana - You are a star! Kudos to you!Dhoti
S
1

You colud use afterSearch option of the search toolbar:

var filteredIDs = new Array(); //Global variable

$("#"+gridId).jqGrid("filterToolbar", { stringResult:true,  searchOnEnter:false,
                                        afterSearch:function(){
                                            filteredIDs = $("#"+gridId).getDataIDs();
                                        }
                                      }); 

If you want to get the filtered rows instead the filtered IDs, use getRowData() instead of getDataIDs().

Sweettalk answered 22/3, 2012 at 20:3 Comment(4)
The problem is that getDataIDs and getRowData get ids of filtered data only from the current page, but one wanted to get all filtered rows, regardless of pagination.Banana
That's true, I'm sorry I didn't read the "regardless of pagination" part. Thanks Oleg.Levorotation
I understand. In any way you choose correct way and your answer is better as typical answer of the person who start on stackoverflow. I would recommend you to include <!-- language: lang-js --> and an empty row before your code (see here and its links for more details)Banana
Done. And yes I'm new here and I would like to help as many people as I can, just like many others helped me indirectly when I was an anonymous user, including you, so thanks again.Levorotation
N
0

All, I found another answer which is far easier to include

loadComplete: function (gridData) {
                    var isSearchPerformed = $grid.getGridParam("postData")._search;
                    if (isSearchPerformed) {
                        $("#spanFilterTotal").text(gridData.records);                        
                }
Nicotinism answered 23/3, 2016 at 16:28 Comment(0)
D
0

All you want is below:

$.each($grid.getRowData(), function( index, value ) {
  a.push(value["COLUMN_NAME"]); //Get the selected data you want
});
Durant answered 27/9, 2017 at 14:41 Comment(1)
Please add explanation along with your code.This helps others better understand your solution.Acerbate

© 2022 - 2024 — McMap. All rights reserved.