How to update jstree node values without reload

Gabor Meszaros picture Gabor Meszaros · Oct 23, 2014 · Viewed 36.9k times · Source

I have a jstree that I created with the following code:

$('#mytree').jstree({"core": { "data" : value
                             , "themes" : { "dots": false
                                          , "icons": false }
                             }
                    }
                   );

I can rebuild it with new data by this code:

$('#mytree').jstree(true).settings.core.data = new_data;
$('#mytree').jstree(true).refresh();

but it can be expensive when you have a lot of nodes. What I would like to achieve is that I would like update the value of the elements (i.e. the node.text part) without rebuilding the whole tree. I get the new values via websocket in one message (the complete JSON string that will be the new_data) but the structure is not changing. How can I do that? Thank you!

Answer

Gabor Meszaros picture Gabor Meszaros · Nov 7, 2014

What you need is not refresh() but redraw() thus the code is

$('#mytree').jstree(true).settings.core.data = new_data;
$('#mytree').jstree(true).redraw(true);

You can find the functions in the jstree API.

As per zmirc suggestion, in v3.1 use:

$('#mytree').jstree(true).settings.core.data = new_data;
$('#mytree').jstree(true).refresh();