I use jqGrid v4.5.4 (free version) to implement tree grid (
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
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.