Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expand 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've got 6 depth-levels and only want 5 to be expanded -- the last one should be excluded.

like image 824
Andreas Zeiner Avatar asked Mar 18 '23 08:03

Andreas Zeiner


2 Answers

    $("#treeView").fancytree("getRootNode").visit(function(node){
        if(node.getLevel() < 3) {
            node.setExpanded(true);
        }
    });
like image 98
Grumpy Avatar answered Mar 25 '23 03:03

Grumpy


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

like image 38
Gone Coding Avatar answered Mar 25 '23 04:03

Gone Coding