Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jstree jquery how to iterate through all nodes

I'm trying to iterate through every node within a treeview in jstree. The treeview is 4 levels deep but I can't seem to get past the 1st level. The following is the jQuery used to iterate.

$("#myTree").bind('ready.jstree', function (event, data) {
    $('#myTree li').each(function () {
        // Perform logic here
        }
    });
});

Here is a jsfiddle illustrating my point. Please help on how I can iterate through every node in jstree.

like image 898
Hshdj122 Avatar asked Jan 27 '16 16:01

Hshdj122


1 Answers

This gets all the children of your tree in a flat array for your .each loop.

$("#tree").bind('ready.jstree', function(event, data) {
  var $tree = $(this);
  $($tree.jstree().get_json($tree, {
      flat: true
    }))
    .each(function(index, value) {
      var node = $("#tree").jstree().get_node(this.id);
      var lvl = node.parents.length;
      var idx = index;
      console.log('node index = ' + idx + ' level = ' + lvl);
    });
});

JSFiddle - Docs for get_json

like image 126
E. Sundin Avatar answered Oct 13 '22 04:10

E. Sundin