Javascript – Is it possible to get all jqGrid rows, across multiple pages, in the currently sorted order

javascriptjqgrid

I'm using the latest version of jqGrid (v4.7.1). I have a grid which is populated with local data and has multiple pages of data. The user has sorted the data client-side. I would now like to retrieve all rows in their sorted order. Is it possible to do this?

Here's what I know:

var data = this.ui.grid.jqGrid('getGridParam', 'data');

This statement returns all rows in the grid, but returns them in their initial state. It is not affected by any sorting operation.

var rowData = this.ui.grid.jqGrid('getRowData');

This statement returns all rows in the current page, but does return them in the properly sorted order.

I was thinking about taking all the data and running it through the grid's sorting function, but that function is heavily guarded. I can get access to it by calling something like:

var data = this.ui.grid.jqGrid('getGridParam', 'data');
$.jgrid.from([])._doSort(data, 0)

However, this code still throws errors as jqGrid expects some other properties to have been set before calling _doSort. I'm confident I can get this working, but it feels like I'm hacking at some code in a really unintended fashion.

What are my options?

EDIT: This works, but it's pretty hacky:

var rowNum = this.ui.grid.getGridParam('rowNum');
this.ui.grid.setGridParam({ rowNum: 10000 }).trigger("reloadGrid");
var data = this.ui.grid.getRowData();
this.ui.grid.setGridParam({ rowNum: rowNum }).trigger("reloadGrid");

Best Answer

The class $.jgrid.from sill be used internally by jqGrid. The usage of of the methods is not documented. In general one should first create an object using $.jgrid.from: for example one can use

var data = $("#mygrid").jqGrid("getGridParam", "data");
var query = $.jgrid.from(data);

Then one should set some internal properties of the object, for example, make the later queries case insensitive

query = query.ignoreCase();

Then one can sort or filter the data. For sorting one should use

query.orderBy("columnNameByWhichOneSort",
    "a", // or "d" for "desc" sorting
    stype,  // sorttype from colModel oder "text"
    srcfmt, // typically ""
    sfunc); // typically null

To get the final results one should use

var queryResults = query.select();

I recommend you to set some breakpoints inside of addLocalData and to debug the code. If you click on come column header the grid will be sorted you you will see how addLocalData uses $.jgrid.from internally.

Probably you can just to follow the answer instead and "subclass" $.jgrid.from. As the result you can get full results (all pages) based on the sorting and searching criteria of the user.

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

Related Topic