Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
86
rated 0 times [  88] [ 2]  / answers: 1 / hits: 19374  / 9 Years ago, wed, january 27, 2016, 12:00:00

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.


More From » jquery

 Answers
22

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


[#63539] Monday, January 25, 2016, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lucianod

Total Points: 667
Total Questions: 106
Total Answers: 92

Location: Jordan
Member since Thu, Aug 5, 2021
3 Years ago
lucianod questions
;