Grady D
Grady D

Reputation: 2039

How to open child node in JsTree when child nodes do not exist until parent is expanded?

I have a parent with multiple children nodes. The problem is if I want to open a specific node I call $("#jstree").jstree("open_node", $('#node_27'));. The problem is that the parent node is not open so the $('#node_27') returns an empty array.

If I have the child ID how can I open it when they are not added to the DOM until the parent is opened?

Upvotes: 1

Views: 3722

Answers (3)

djuka
djuka

Reputation: 46

If you use ajax to populate tree, there is lazy loading of child nodes. You must first use 'load_node' to load parent node and then use 'select_node' for children node.

$('#jstree').on('refresh.jstree', function () {
    $('#jstree').jstree(true).load_node('parent-id', function () {
        $('#jstree').jstree().deselect_all();
        $('#jstree').jstree('select_node', 'child-id');
    });
});
$('#jstree').jstree(true).refresh(false, false);

Upvotes: 0

Nikolay Ermakov
Nikolay Ermakov

Reputation: 5061

The _open_to method is a private one and is not supposed to be used directly. Though working right now it may change in the future, e.g. when the jsTree author decides to change the internal mechanics of his library.

To open parents down to the node you can simply use

$('#jstree1').jstree('select_node', 'node_27' );

As a bonus you will get the target node selected.

Upvotes: 1

juvian
juvian

Reputation: 16068

Apparently if you want to "deep open" a node, you would need to use

$("#jstree1").jstree("open_node", ['rootId','childId','childChildId','yourNodeId']);

However, there is a method called _open_to that already does that automatically, so you can do:

$("#jstree1").jstree("_open_to", 'yourNodeId'); //j1_5 in your case

And it will open whatever it needs to so as to reach the node you want

Upvotes: 1

Related Questions