3

I have a file upload element that scans the file names upon change and does something to get a valid treeJSON variable. When the function detects a change the second time, the treeJSON variable will update, and I need to update the tree.

I've tried various methods such as refresh(), destroy() etc., but I haven't been able to construct a new tree on the same page without reloading the page. Here is a snippet, I've included all the functions and it works. The important part is working with the treeJSON

input.onchange = function (e) {
  // do something
};
var treeJSON = someVar;

$("#tree").jstree({
  core: {
    check_callback: true,
    data: treeJSON,
  },
});
Puka
  • 1,485
  • 1
  • 14
  • 33
  • Does this answer your question? [How can I refresh the contents of a jsTree?](https://stackoverflow.com/questions/3682045/how-can-i-refresh-the-contents-of-a-jstree) – Puka Jul 06 '22 at 13:15

1 Answers1

1

You only have to tell jstree once where to load the root (core.data) . On refresh the tree will re-populate based on this content.

I would do this:

var treeJSON = 'originaltree';

input.onchange = function (e) {
    // something
    treeJSON = $.parseJSON(newtree); // if newtree is json
    var tree = $('#tree').jstree(true);
    tree.refresh();
}

$('#tree').jstree({
    'core': {
        'check_callback': true,
        'data':  treeJSON
    }
});

Or if you want to be even more specific based on which node:

$('#tree').jstree({
    'core': {
        'check_callback': true,
        'data': function (node, cb) {
            if (node.id === '#') { //Load root, will fire on first load as well as on refresh
                cb.call(this, treeJSON);
            }
        }

    }
});
Cat
  • 56
  • 4
  • Ok how doesn't it work? If newtree is json, did you parse it? http://www.jstree.com/api/#/?q=refresh&f=refresh() – Cat Feb 17 '15 at 10:16
  • Have a look at my function : http://jsfiddle.net/bsw5s60j/ I tried doing as u said. It doesnt work when I add it to this –  Feb 17 '15 at 12:35
  • And for some reason.. This tree itself doesnt work now! I dont remember changing anything! Going to tke another hour of brain storming I suppose. (UPDATE). My tree works now. But therefresh you suggested doesnt –  Feb 17 '15 at 12:46
  • Ok, check the fiddle again I have updated it so it refreshes. Seems that the cb.call() was the trick that did it. http://jsfiddle.net/bsw5s60j/4/ – Cat Feb 18 '15 at 07:15