Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fancytree activate node on button click

I have a fancy tree solution on my website and i would like to have a button that will trigger a specific node.

Can i activate a specific node from a button click or trigger it after the fancy tree has loaded?

My fancytree code:

  $("#tree").fancytree({ //Fancy Tree
        checkbox: false,
        selectMode: 3,
        extensions: ["dnd"],
        source: {
            url: "@(Url.Action("GetCategoryForFancyTree", "LinksDocuments"))" + '?time=' + timestamp,
                success: function(data){

                    console.log(data);
                },
                cache: true
            }
     });

I saw that this code maybe i could use but i dont dont know the node key for the node, how could i retrieve the key?

$("#tree").fancytree("getTree").getNodeByKey("id4.3.2").setActive();
like image 982
Peter Avatar asked Feb 11 '23 01:02

Peter


1 Answers

The .getNodeByKey is just expecting the "id" of the node element. For example to get the "Sample Node" from the following example just set the id for each element:

<div id="tree">
    <ul>
        <li id="123">Sample Node</li>
    </ul>
</div>

And use something like this to activate it:

$("#tree").fancytree("getTree").getNodeByKey("123").setActive();

I recommend adding this to the "init: function() {}" otherwise it may not activate if the tree is still loading/building.

Can also use the following from within the "init:", should do the same.

data.tree.activateKey("123")

Finally, to get the key if you don't already know it:

click: function (event, data) {
    var node = data.node;
    alert("ID: " + node.key);
}
like image 85
notImportant Avatar answered Feb 13 '23 16:02

notImportant