I have loaded a jsTree with an AJAX call that returns JSON data. How can I refresh the tree so that it reloads its contents?
5 Answers
At version 3 you can reload the tree :
$('#treeId').jstree(true).settings.core.data = newData;
$('#treeId').jstree(true).refresh();
-
3This works for me, as I JSON data coming from some other function and not AJAX– TechMazeApr 23, 2015 at 20:33
-
I had to set the second parameter of refresh to true: tree.jstree(true).refresh(false, true); Dec 5, 2016 at 23:48
-
when using AJAX i had to chain
.url
->jstree(true).settings.core.data.url = "/new/data/url"
– oklivMar 11, 2017 at 13:24
var tree = jQuery.jstree._reference("#files");
tree.refresh();
or
var tree = jQuery.jstree._reference("#files");
var currentNode = tree._get_node(null, false);
var parentNode = tree._get_parent(currentNode);
tree.refresh(parentNode);
-
3I needed to refresh just one specific node. Thanks for this useful tip. Oct 12, 2012 at 3:00
-
2
for jstree3 . I use destroy() function and again create tree running jstree() function
-
7
-
@IonicăBizău It's both an event and a method... but that was the clue I needed. You can't call the destroy() method until the tree is initialized, but you can trigger the event.– AuspexOct 12, 2020 at 14:54
$('#treeId').data('jstree', false).empty().jstree(json);
-
It does not work for jstree:3.3.9. You get an error
Uncaught TypeError: $jstree.data(...).empty().jstree is not a function
– VityFeb 15, 2021 at 7:30