Change nodes

Hello,

I use GoJS and the TreeModel.

Each node in my tree has a property called “level” that simply is a number that shows its level in the tree. When I move a sub-graph the value of this “level” should get adjusted in all nodes of that sub-graph. How to do it?

I have this inside a mouseDrop-Function:

var diagram = node.diagram;
var selnode = diagram.selection.first();  // assume just one Node in selection
if (mayWorkFor(selnode, node)) {

      diagram.model.startTransaction("adjust level of all nodes under the Selected Node");
          var itr = selnode.findNodesOutOf();
          while (itr.next()) {
            var nodeN = itr.value;
            // here I need:     var nodeNadjustedLevel = find out the level-value of the nodeN
           diagram.model.setDataProperty(nodeN.data, 'level', nodeNadjustedLevel);
               }
        diagram.model.commitTransaction("adjust level of all nodes under the Selected Node");

}

selnode is the node that gets moved. If selnode has a sub-graph hanging under it then this sub-graph moves along with it --> all its nodes should get the adjustment of their “level” property.

By the way, you don’t need to conduct your own transaction in a mouseDrop event handler. GraphObject | GoJS API

I tried

var nodeNadjustedLevel = nodeN.findTreeLevel();

and it doesn’t work: Only the level-number of the selnode and of its direct child/children gets updated, not of all nodes of the subgraph that is hanging under the selnode. How would be possible to solve this problem?

You should be able to recurse through the subtree calling findNodesOutOf on each child, just as you’re already doing with the root.

@jhardy: Is there a similar example in the GoJs documentation?

I’m sure some of our samples use recursion, but I went ahead and made a simple example similar to your usage.

See http://codepen.io/jhardy/pen/RoLmOV?editors=1010.

Note the use of the recursive setSubtreeLevels function which visits the children of the parameter node, setting their levels.

Thanks A LOT xxx