14

I am trying to implement a very simple tree using jsTree. I have found the documentation dense and overwhelming.

Right now, I expand / collapse a node by clicking the arrow shown here:

enter image description here

I want to be able to expand / collapse by clicking the node name too:

enter image description here

The code I am using is simple; I have not altered the javascript for jsTree:

<ul id="tree">
   <li>
      SubFolder1
      <ul id="tree">
         <li data-jstree='{"icon":"/Images/blue-folder.png"}'>Pub 1</li>
      </ul>
   </li>
</ul>
John 'Mark' Smith
  • 2,564
  • 9
  • 43
  • 69
  • possible duplicate of [Open branch when clicking on a node?](http://stackoverflow.com/questions/4486032/open-branch-when-clicking-on-a-node) – kostmo Dec 07 '14 at 04:01

3 Answers3

22
$('#tree').on('select_node.jstree', function (e, data) {
    data.instance.toggle_node(data.node);
});

That worked for me. oerls solution did not.

itmuckel
  • 1,300
  • 15
  • 23
9

Just add an event listener in your html file and call the toggle_node function. This code below listens for a single click.

$(document).ready(function(){
  $('#jstree_div').on("select_node.jstree", function (e, data) { $('#jstree_div').toggle_node(data.node); });
}

If you want to listen for a double click you need another event listener, since jsTree does not support double click events yet.

$('#jstree_div').on("dblclick",function (e) { 
  var li = $(e.target).closest("li");
  var node = $('#jstree_div').get_node(li[0].id);

  $('#jstree_div').toggle_node(node)
});

Hope that helps.

oerl
  • 1,204
  • 14
  • 16
  • 1
    It does support double click now [link](https://www.jstree.com/api/#/?q=$.jstree.defaults&f=$.jstree.defaults.core.dblclick_toggle) – chech May 12 '16 at 11:25
4
$('#jstree').on("select_node.jstree", function (e, data) { 
     $('#jstree').jstree("toggle_node", data.node);
 });

also this will work