JSTree - How to make tp user select only tree leaves

In my JStree, I want the user to be able to select only the leaves of the tree. For example: Nodes that have no children. My idea is to bind the select event and manually check if the selected node has children, and then select / not select node accordingly.

Is there an easier way? or is this the only obvious solution?

+5
source share
4 answers

you can use

  • Plugin type
  • .is_leaf () to check if the selected node is a node (leaf) or not. Return false to 'before' - The event will be rejected by selecting node. See 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; } }) 
+2
source

2014 - version 3.0.1

 $('#jstree').on('activate_node.jstree', function(e, data) { if(data.instance.is_leaf(data.node)) { ... } }); 
+6
source

A simple solution to use the CheckBox plugin (for RadioButton behavior) here

 $('#treeview').bind('activate_node.jstree', function (event, data) { if (data.instance.get_checked().length > 1) { data.instance.uncheck_all(); } }); 

In this jquery.

+2
source
 $('#jstree').on('activate_node.jstree', function(e, data) { if(!data.instance.is_leaf(data.node)) { data.instance.deselect_node(data.node, true); } } 

This code deactivates node if node is not a leaf.

Thus, only leaves are selected.

0
source

Source: https://habr.com/ru/post/1386283/


All Articles