Expand the Tree to a specific level

How can I use node.setExpanded(true);

to expand the tree only to a certain level?

What I mean is that I have 6 depth levels and only need 5 to be expanded - the last one should be excluded.

+3


source to share


2 answers


This is just a guide, since you didn't provide any sample data or code, but you can use a recursive function to iterate through the tree, stopping when a certain 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 root node and total depth:



expandNode(rootNode, 5);

      

There might be better techniques for iterating fancytree, but I haven't used fancytree before

+1


source


    $("#treeView").fancytree("getRootNode").visit(function(node){
        if(node.getLevel() < 3) {
            node.setExpanded(true);
        }
    });

      



+2


source







All Articles