collapseTree

I have this org chart:
http://gojs.net/latest/samples/orgcharteditor.html

I would like to show only Stella, Luke and Peggy, and hide (collapse) others. Where do I put collapseTree(1) in this code to make this happen? I am also using $(“TreeExpanderButton”) on each node.

Thank you for your reply

After you have assigned the Diagram.model, you could do something like:
myDiagram.findTreeRoots().each(function(n) { n.collapseTree(2); });

The presence or absence of a TreeExpanderButton will not affect anything other than the ability for users to control the expansion state of individual nodes.

Thank you very much for your answer. This works; although I woud need to set this setting only to the first node.

So what I would need is
n.collapseTree(2); for the top node
and n.collapseTree(1); for every other node. Top node does not have “boss” parameter, others do.

Can you help me with this as well?

Calling Node.collapseTree(2) on the root Node will result in at most 2 layers of the tree being visible.

myDiagram.findTreeRoots().each(function(n) { if(n.data.boss==null) {n.collapseTree(2);} });    

Calling this shows two levels by default-which is ok, but when trying to open another level, it opens two more levels-not one.

The issue is that Node.wasTreeExpanded remembers the previous expansion state.

The easy solution is to call root.collapseTree(2) and then:
myDiagram.nodes.each(function(n) { n.wasTreeExpanded = false; })

Alternatively you could change the click event handler of the “TreeExpanderButton” to only expand a single level.

“ReferenceError: root is not defined”

I guess I’m just not that good at that Cry

How do I get the root node?

You have already called it: Diagram.findTreeRoots().

Okay, my mistake. That worked. I tried all combinations but not together.

Thank you very much for all your (fast!) replies SmileWink