4

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");
Sean Anderson
  • 27,963
  • 30
  • 126
  • 237

1 Answers1

4

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.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks oleg! Any thoughts on jqGrid having a method out-of-the-box which returns all rows w/ proper sorting instead of having to dig for it? It seems like it has been a common issue for several years. – Sean Anderson Jan 09 '15 at 20:18
  • 2
    @SeanAnderson: I think so too. As you probably knows there are some forks of jqGrid after changing of license agreements. I've implemented already some new features (see [the post](https://github.com/OlegKi/jqGrid-plugins/issues/1#issuecomment-69001575) for the base information about one new feature) and have fixed many bugs of jqGrid 4.7 in [my fork](https://github.com/OlegKi/jqGrid). I will include the suggestion in my TODO list. I will inform you after I have the feature implemented. – Oleg Jan 09 '15 at 20:34
  • Excellent! You'll be happy to know a lot of developers over here in our team are stoked to hear that ;) :D Good luck and looking forward to it. – Sean Anderson Jan 09 '15 at 21:35
  • 1
    @SeanAnderson: The implementation was very simple and I did this already. See [the demo](http://www.ok-soft-gmbh.com/jqGrid/OK/GetFilteredData.htm) and the corresponding code [changes](https://github.com/OlegKi/jqGrid/commit/f1d51b32338863007e1d0c7bd3f8281bc881787e) in my jqGrid fork. So the first version of my fork (which I plan soon) will already have the feature. You can anytime download my current development state from [github](https://github.com/OlegKi/jqGrid) and use just `jquery.jqGrid.src.js`, `jquery.jqGrid.min.js` and `jquery.jqGrid.min.map`. – Oleg Jan 09 '15 at 22:54