Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update jstree node values without reload

Tags:

jstree

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!

like image 345
Gabor Meszaros Avatar asked Oct 23 '14 16:10

Gabor Meszaros


3 Answers

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();
like image 189
Gabor Meszaros Avatar answered Nov 14 '22 06:11

Gabor Meszaros


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);
like image 2
baaroz Avatar answered Nov 14 '22 05:11

baaroz


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']
                                }
                            ]

                     }
                });

            }
like image 2
Aziz Nortozhiev Avatar answered Nov 14 '22 06:11

Aziz Nortozhiev