Question

I have been trying to find out how to check if a node is a leaf in a jstree, but all the information out there seems to be about previous versions of jstree and obsolete as far as I can tell...

So I have the following:

$('#jstree').jstree();

$('#jstree').on("changed.jstree", function (e, data) {
    console.log(data.selected);

});

The change callback gives me an output of the selected ids in the console. But how do I get information about whether they are leaves (have no children)?

I suppose it would have to loop through some array, because the above can provide ids for multiple selected nodes (which I want), like so in the console:

["3", "4"]

But I can't figure out how to do this. I've been searching the documentation too, but for my level of jquery skills it doesn't help, it just seems to list the objects and events and so on (for example "is_leaf" is listed under the "types plugin"), but I can't find examples of how to code it. I have no idea where to put either the types plugin if that's what I need, nor the is_leaf part in order to get what I described above for each selected node...

Was it helpful?

Solution 2

Every leaf node in jstree has the

  class='jstree-leaf'

in its html. So you could check this in the

$.each(function(e,data){ });

in the

  .bind('loaded.jstree',function(e,data){

   //code here

   });

to check whether the class exists in the tree or not. The each loop in the loaded event traverses through all the nodes in jstree.

Hope you got some idea. Rest you can refer the documentation for syntaxes.

Let me know in case of any queries.

OTHER TIPS

Just use the is_leaf function on the node.

http://www.jstree.com/api/#/?q=is_leaf&f=is_leaf(obj)

if ($('#jstree').jstree().jstree.get_selected(true)[0].children == 0)
    //Do Something

So this will check if the selected item has any children, if not then proceed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top