Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggle entire jQuery jstree

Tags:

jquery

jstree

Is it possible to open and close the entire tree through a button?

I know that in order to open all the nodes and subnodes of the tree, I have to call the open_all function like : $("#tree").jstree('open_all');

And in order to toggle a node : $("#tree").jstree("toggle_node","#1"); where #1 is the id of the first child.

But the toggle function does not expand all the subnodes of the node. Nor does it open a half-opened tree. I can call open_all and close_all on button click, but how do I find which method to call, as in figure out if the tree has to be opened or closed?

like image 420
imgr8 Avatar asked Dec 06 '22 20:12

imgr8


2 Answers

Pass -1 for the entire tree:

$("#tree").jstree("open_all", -1);

To close all you can use the close_all function:

$("#tree").jstree("close_all", -1);

You can also use save_opened to remember which nodes are opened and then reopen them later with the reopen function.

Look at the documentation.

like image 77
Petar Ivanov Avatar answered Dec 11 '22 09:12

Petar Ivanov


$("#treepanel").jstree("open_node", $('li[id="' + nodeId + '"]'), function() {
   alert("node is added")
});

try this

like image 40
Artur Keyan Avatar answered Dec 11 '22 10:12

Artur Keyan