20

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!

Gabor Meszaros
  • 1,335
  • 1
  • 15
  • 25

7 Answers7

32

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();
Kriggs
  • 3,731
  • 1
  • 15
  • 23
Gabor Meszaros
  • 1,335
  • 1
  • 15
  • 25
  • 5
    With v 3.1.0 it doesn't seem to work, unless redraw(true) is replaced with refresh(). – zmirc Apr 28 '15 at 19:07
  • 3
    I have been trying to use this and it seems to work but the state of the objects does not seem to be updated. Is it possible to force the newly drawn tree to conform to state options provided in the new_data? – OganM Nov 20 '15 at 01:05
2

for deleting the node and reload tree

 $('#mytree').jstree(true).refresh();

for those who need to redraw without restart the tree use

jQuery('#data').jstree(true).refresh(true);
baaroz
  • 19,119
  • 7
  • 37
  • 47
2

worked for me: $('#structureRows').jstree("destroy").empty();

function CreateStructureTree(jsonData)
        {
            $('#structureRows').jstree("destroy").empty(); 
            $('#structureRows').jstree
                ({
                    'core' : {
                        'data':
                            [

                                jsonData,
                                {
                                'id' : 'node_2',
                                'text' : 'Root node with options',
                                'state' : { 'opened' : true, 'selected' : true },
                                'children' : [ { 'text' : 'Child 1' }, 'Child 2']
                                }
                            ]

                     }
                });

            }
Aziz Nortozhiev
  • 365
  • 5
  • 8
1

You can refresh node by this

$('#treeView').jstree(true).refresh_node("node_id_here")
Atif
  • 278
  • 1
  • 3
  • 16
0

$('#mytree').jstree(true).refresh(); is working, but in my case it causes thread leak. every refresh adds one more thread

Shlomi Aharoni
  • 482
  • 7
  • 9
0

I load data via an url, so my refresh part looks like:

$('#groupTree').jstree(true).settings.core.data.url = get_group_url();
cwhisperer
  • 1,478
  • 1
  • 32
  • 63
-2
  1. $('#YourJSTREE').jstree("destroy").empty();
  2. Set new data
  3. $('#YourJSTREE').jstree(true).refresh();
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129