1

I use jqGrid v4.5.4 (free version) to implement tree grid (treeGrid:true and treeGridModel:'adjacency') and everything works perfectly.

Now I want to implement server side sorting only on the child nodes but from this question, I understand that the sorting is always done locally on client for treeGrid.

Is there any work around for this behavior and make a server request for sorting and reload the whole treeGrid with sorted data from server?

Thanks in advance!

Community
  • 1
  • 1
DMv2
  • 198
  • 1
  • 12

2 Answers2

0

The child nodes will be included im same order as returned from the server if the data will be loaded initially. I can suggest you to implement removing of child nodes on collapsing the node. You main problem is the usage of retro version 4.5.4. If you would update to free jqGrid 4.13.5 then you could solve your problem by including unloadNodeOnCollapse: true option. It will automatically remove all child nodes on collapsing of the parent node. One can even implement more complex scenario by usage callback function, which returns boolean, instead of usage unloadNodeOnCollapse: true option. The demo shows how you cab do this.

The usage of retro version makes the problem more complex. You even can't use afterCollapseNode or beforeCollapseNode callbacks, called on collapsing the node, because the callbacks are not implemented in jqGrid 4.5.4. The only solution way would be subclassing collapseNode method. You can follow the old answer to implement subclassing. You can first call original method and then call delTreeNode, but be careful, because the method delete the collapsing node too (delTreeNode has no second option which allows to delete only children). Thus you have to save the collapsing node and to add it directly after calling of delTreeNode or alternatively you can call getFullTreeNode and then call delRowData in the loop skipping the collapsing node.

You can see that implementing of the requirements in the retro version jqGrid 4.5.4 isn't simple. Thus I'd recommend you to consider to update to the current version free jqGrid. You will get additional performance advantage for free.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thank you very much @Oleg for your quick response. We have been thinking of upgrading our jqGrid libraries for sometime now. Though I'm not sure if it will solve the server side sorting as I want a similar behavior of regular jqGrid on childnodes i.e. on click of column headers and the tree can have all root nodes expanded when sorting is applied (column header is clicked). – DMv2 Nov 25 '16 at 14:44
  • @Deb_: Do you load **all nodes with child notes at once**? You current requirement implies reloading currently loaded noded from the server in the new sorted order. It makes the interface to the server more complex. I'm not sure **why** you want to sort on the server side? You can customize *client side sorting* instead in any way. – Oleg Nov 25 '16 at 14:52
  • Yes, I load all nodes with child nodes at once. Our assumption is that we won't have too many records at first place (<200, including all parents and child nodes). I want to sort only the leaf node rows on click of column header, so I was thinking of completely reloading jqGrid with sorted data again. Can I achieve this in client side sorting? Thanks. – DMv2 Nov 25 '16 at 15:00
  • 1
    @Deb_: It's difficult for me to understand your problem because you don't posted any code or any test data. Could you explain **why** you don't want to sort the data on the client side? During local sorting jqGrid uses `sorttype` or `sortfunc`, which you can define as callback functions and implement your custom sorting. Alternatively you can use `onSortCol`, which return `"stop"` to prevent local sorting, where you can call `clearGridData` and `reloadGrid` additionally to reload the data from the server. – Oleg Nov 25 '16 at 15:49
  • I think `onSortCol` is something I can use to reload the grid, I'm going to try this option. Thanks a ton. PS: I have been using jqGrid for sometime now and your answers have saved so much time for me. :) – DMv2 Nov 25 '16 at 16:20
  • 1
    I glad, that my old posts helped you too. You are welcome! – Oleg Nov 25 '16 at 16:31
0

Based on inputs from @Oleg, this is what I did to implement sever side sorting for tree in jqGrid

(onSortCol event is raised immediately after sortable column is clicked, more on this event here)

onSortCol: function (index, iCol, sortOrder) { $('#'+gridId).jqGrid('setGridParam', {postData:formData}).trigger('reloadGrid'); return 'stop'; //stops the sort processing }

DMv2
  • 198
  • 1
  • 12