Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get node by ID in Jstree

Tags:

jquery

jstree

I have created a jstree and i have a problem with getting node by id of jstree. when i use get_node, i get an error:

TypeError: $(...).jstree.get_node is not a function

this is html code:

<div style="height: 75%; margin: 0; width: 100%;">
                    <div id="dashboardTree" style="border: 0; height: 99%; margin: 0; margin-top: 2px; overflow: auto; width: 99%;">
                    </div>
                </div>

this is the javascript:

$(document).ready(function () {
initDashboardArchiveTree();//Initial tree
var node = $('#dashboardTree').jstree(true).get_node('1')//get that error
});

How to get node by id in jsTree?What's wrong with this code?

like image 760
ZSH Avatar asked Jul 21 '15 23:07

ZSH


3 Answers

Try this:

var node = $('#dashboardTree').jstree(true).get_node('1, true')

New addition: true

OR

Change this:

var node = $('#dashboardTree').jstree(true).get_node('//something')

To this:

var node = $('#dashboardTree').jstree(true).find('//something');

Get the JSON of the parent and find the children.

Read the documentation on jstree/JSON.

like image 117
Lansana Camara Avatar answered Nov 07 '22 17:11

Lansana Camara


To get the node use this:
$('#dashboardTree').jstree(true).get_node('1');

If you need the actual DOM node, use this: $('#dashboardTree').jstree(true).get_node('1', true);

But only invoke this once the tree is ready:

$('#dashboardTree').on('ready.jstree', function (e, data) {
  var node = data.instance.get_node('1');
})
initDashboardArchiveTree(); //Initial tree
like image 8
vakata Avatar answered Nov 07 '22 16:11

vakata


should help this one:


.on("hover_node.jstree", function (e, data) {
    let node_id = data.node.id
    let block   = data.instance.get_node(node_id, true)
...
like image 1
1210mk2 Avatar answered Nov 07 '22 17:11

1210mk2