Layui

Get all rows not filtered from jqGrid

administrator 提交于 2018-03-17 00:32:19

问题:

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?

回答1:

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.



回答2:

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().



回答3:

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


回答4:

All you want is below:

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


添加新评论

这个问题是为了测试你是否是一个人类访问者并防止自动垃圾邮件提交

5 + 6 =
计算出这道简单的算术题并键入答案。例如、1+3,就输入 4。