4

How can I use node.setExpanded(true); to expand the tree only to a certain level?

What I mean is that I've got 6 depth-levels and only want 5 to be expanded -- the last one should be excluded.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Andreas Zeiner
  • 537
  • 7
  • 24

2 Answers2

5
    $("#treeView").fancytree("getRootNode").visit(function(node){
        if(node.getLevel() < 3) {
            node.setExpanded(true);
        }
    });
Grumpy
  • 2,140
  • 1
  • 25
  • 38
2

This is just a guide, as you have provided no sample data or code, but you can use a recursive function to iterate the tree, stopping when a specific depth is reached. Something like:

function expandNode(node, depth) {
    // Expand this node
    node.setExpanded(true);
    // Reduce the depth count
    depth--;
    // If we need to go deeper
    if (depth > 0) {
        for (var i = 0; i < node.children.length; i++) {
            // Go recursive on child nodes
            expandNode(node.children[i], depth);
        }
    }
}

and call it with the root node and overall depth:

expandNode(rootNode, 5);

There may well be better methods to iterate a fancytree, but I have not used fancytree before

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
  • thanks a lot, sorry for not providing any code. i've used $("#tree").fancytree("getRootNode").visit(function(node){ node.setExpanded(true); }); for expanding every node beginning from the rootnode! – Andreas Zeiner Oct 01 '14 at 13:47
  • @Andreas Zeiner: Code and example html would have meant a working demo, but you get the idea from this example I hope :) – iCollect.it Ltd Oct 01 '14 at 13:48
  • works perfekt! you're a star and may the last ninja be with you :-) yes a played them all on the c64 - been there since 1985! – Andreas Zeiner Oct 01 '14 at 14:28
  • you could also use `node.visit(...)`and `if( node.getLevel() < 6)...` – mar10 Oct 01 '14 at 16:30
  • @mar10: That's the `fancytree` specific stuff I don't know about. Why don't you post an answer? – iCollect.it Ltd Oct 01 '14 at 16:32