Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSTree - How to force user tp only select leafs of the tree

Tags:

jquery

jstree

Within my JStree i want the user to only be able to select leafs of the tree. E.g.: Nodes that have no childs. My idea is to bind the select event and manually check whether the selected node has childs and then select/not select the node accordingly.

Is there a simpler way? or is this obvious solution the only one?

like image 294
Manuel Schmidt Avatar asked Dec 14 '11 10:12

Manuel Schmidt


2 Answers

2014 - version 3.0.1

$('#jstree').on('activate_node.jstree', function(e, data) {
  if(data.instance.is_leaf(data.node)) {
    ...
  }
});
like image 180
Tab10id Avatar answered Oct 29 '22 14:10

Tab10id


You can use

  • Types plugin
  • .is_leaf() for checking if the selected node is a child- node (leaf) or not. Returning false in 'before'-Event will reject selecting the node. See the jsTree groups

In the code.

$('#treeID') 
.bind('before.jstree', function(event, data){ 
        switch(data.plugin){ 
                case 'ui': 
                        if(data.inst.is_leaf(data.args[0])){ 
                                return false; 
                        } 
                        break; 
                default: 
                        break; 
        } 
}) 
like image 33
Radek Avatar answered Oct 29 '22 12:10

Radek