Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I identify a node as being a root node?

I want to create a node inside or under an existing node depending on if it is a root node. (a tree widget is usually a list of trees or a tree without a visible root node.)

I tried get_parent, but how do I know if that is a root node?

var parent = $("#demo1").jstree('_get_parent', $("#foo"));
var node = $("#demo1").jstree('_get_node', $("#foo"));

What confused me is that get_node seems to return the same object as get_parent.

I'm using jstree_pre1.0_fix_1.

edited:

I ended up checking for the known id of the parent of the parent.

var node = $(e.replyto);
if (node.length) {
  if (node.parent().parent().attr('id') == 'demo1') {
    $("#demo1").jstree("create_node", node, 'last',{'attr': {'id':e.id}, 'state':'open', 'data': e.data}) ;
  } else {
    $("#demo1").jstree("create_node", node, 'after',{'attr': {'id':e.id}, 'state':'open', 'data': e.data}) ;
  }
} else {
    $("#demo1").jstree("create_node", -1, 'after',{'attr': {'id':e.id}, 'state':'open', 'data': e.data});
}
like image 482
Eddy Pronk Avatar asked Oct 27 '25 04:10

Eddy Pronk


1 Answers

You can call get_parent() on the node. If it returns '#' then the node is a root node. E.G:

var node = ...;

if($('#demo1').jstree(true).get_parent(node) == '#') {
    // node is a root node
}
like image 191
Nada_Surf Avatar answered Oct 29 '25 17:10

Nada_Surf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!