Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsTree Open a branch

I am using the JQuery plugin jsTree, http://www.jstree.com/ I am able to expand the whole tree with the following method:

$("#tree").jstree("open_all");

and also a specific node:

$("#tree").jstree("open_node", $('#childNode'));

I am having difficulty opening a branch of the tree, open branch opens it fine but does not open its parent if it has one.

Has anyone successully done this with jsTree? Let me know if you need more info.

Thanks

Eef

like image 620
RailsSon Avatar asked Nov 13 '10 05:11

RailsSon


3 Answers

Your code for open branch is correct.

For example. Source of tree:

    <div id="treeTask">
       <ul>
          <li id="node_37"><a href="#">TEST1</a>
              <ul>
                  <li id="node_38"><a href="#">TEST2</a></li>
                  <li id="node_39"><a href="#">TEST3</a></li>
              </ul>
          </li>
      </ul>
   </div>

Open node:

$("#treeTask").jstree("open_node", $("#node_38"));
like image 117
KiriLL Ivanov Avatar answered Nov 20 '22 07:11

KiriLL Ivanov


Try this code to open node till nth Level

$("#myTree").jstree({options}).bind('loaded.jstree', function (e, data) {
    /** 
     * Open nodes on load (until x'th level) 
     */
    var depth = 3;
    data.inst.get_container().find('li').each(function (i) {
        if (data.inst.get_path($(this)).length <= depth) {
            data.inst.open_node($(this));
        }
    });
});
like image 10
Arvind Avatar answered Nov 20 '22 05:11

Arvind


You could use the binding

$("#tree").bind("open_node.jstree", function (event, data) { 
  if((data.inst._get_parent(data.rslt.obj)).length) { 
    data.inst._get_parent(data.rslt.obj).open_node(this, false); 
  } 
}); 
like image 9
Bob Avatar answered Nov 20 '22 07:11

Bob