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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With