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?
Asked
Active
Viewed 9.0k times
5 Answers
73
At version 3 you can reload the tree :
$('#treeId').jstree(true).settings.core.data = newData;
$('#treeId').jstree(true).refresh();

aecavac
- 1,026
- 8
- 7
-
3This works for me, as I JSON data coming from some other function and not AJAX – TechMaze Apr 23 '15 at 20:33
-
I had to set the second parameter of refresh to true: tree.jstree(true).refresh(false, true); – jeffslofish Dec 05 '16 at 23:48
-
when using AJAX i had to chain `.url` -> `jstree(true).settings.core.data.url = "/new/data/url"` – okliv Mar 11 '17 at 13:24
26
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);

Aigiz
- 381
- 3
- 3
-
3I needed to refresh just one specific node. Thanks for this useful tip. – DavidHyogo Oct 12 '12 at 03:00
-
2
12
for jstree3 . I use destroy() function and again create tree running jstree() function

bydan
- 121
- 1
- 2
-
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. – Auspex Oct 12 '20 at 14:54
2
$('#treeId').data('jstree', false).empty().jstree(json);

Kedron
- 343
- 1
- 3
- 9
-
It does not work for jstree:3.3.9. You get an error `Uncaught TypeError: $jstree.data(...).empty().jstree is not a function` – Vity Feb 15 '21 at 07:30