Get all rows not filtered from jqGrid

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

For example, let's say I start with 50 lines in a grid. The user uses the filter toolbar, and the rowset is reduced to 10 lines. How can I get these ten lines?

+6
source share
4 answers

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

For this, I suggest using a subclass: rewriting the select method of the $.jgrid.from method. I will demonstrate the technique in the examples created to answer this as well .

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

The lastSelected variable lastSelected saves an array of elements that are the results of the last sort or filter operation. Since $.jgrid.from is global, the data is not grid related. If you have more than one grid per page, this will be inconvenient. You can fix a minor flaw with the following line in the loadComplate code of each grid:

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

We introduce the new parameter jqGrid lastSelected , which will have a tight structure as data , but will contain only the last filtered data.

The following code will display the identifiers of the filtered data in an 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 the localReader.id parameter because the property name used for local data is usually id or _id_ . _id_ will be used when loading data from the server if the loadonce: true option is used.

Demo demonstrates the approach. If for one filter, for example, only data from FedEx, and then click the "Show Identifiers" button, you will see information about all filters, and not just about the data displayed on the current page:

enter image description here

enter image description here

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

+23
source

You can use the afterSearch function for the search bar:

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

If you want to get filtered strings instead of filtered identifiers, use getRowData () instead of getDataIDs ().

+1
source

That's it, I found another answer, which is much easier to include

 loadComplete: function (gridData) { var isSearchPerformed = $grid.getGridParam("postData")._search; if (isSearchPerformed) { $("#spanFilterTotal").text(gridData.records); } 
0
source

All you need is below:

 $.each($grid.getRowData(), function( index, value ) { a.push(value["COLUMN_NAME"]); //Get the selected data you want }); 
0
source

Source: https://habr.com/ru/post/911125/


All Articles