62

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?

John Mills
  • 10,020
  • 12
  • 74
  • 121

5 Answers5

78

Turns out is is as simple as calling:

   tree.jstree("refresh");
John Mills
  • 10,020
  • 12
  • 74
  • 121
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
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
12

for jstree3 . I use destroy() function and again create tree running jstree() function

bydan
  • 121
  • 1
  • 2
  • 7
    Actually, it's an event, but it helped. `tree.jstree("destroy")` – Ionică Bizău Jun 19 '14 at 05:35
  • @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