Your code seems to be operating on all of the nodes, so the order in which you operate on them will affect the results. And it’s very inefficient too. And you should never perform a transaction in the body of a loop.
Why not only operate only on root nodes? Call Diagram.findTreeRoots.
In fact, why not set it up so that by default everything is collapsed?
Yes, I have iterated through nodes and take that 2nd node which is r.data[‘isRg’] and for that only I have applied CollapseTree().…
Can you please give me what should I do??
I was suggesting that you adjust your templates so that everything starts off collapsed. The only code you would need to write would be find and expand the one root node that you care about.
In your node template, set Node.isTreeExpanded to false. This will cause everything to be collapsed. Then you only need to expand a particular tree.
Is that Node.isTreeExpanded property data bound in your node template? If so, then all you need to do is set it source data property to true on the node data object of the root node that you want to be expanded.
If that property is not data bound, then do something like:
var root = myDiagram.findNodeForKey(...);
if (root) root.expandTree();