Deb_ Deb_ - 14 days ago 9
jQuery Question

How to implement server side sorting for jqGrid tree?

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!

Answer

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.