vakata / jstree

jquery tree plugin
http://jstree.com
MIT License
5.13k stars 1.39k forks source link

cannot use create_node for nodes which were created by create_node themselves #2727

Closed kunibertgangnam closed 1 year ago

kunibertgangnam commented 1 year ago

Hi, how can I create child nodes for nodes which were created by myself (by using create_node) ? I am using jsTree - v3.3.12

basically my code looks like this (pseudocode):

function createBranch(parentId, nodes) {
    var parent = parentId;
    for (var i = 0; i < nodes.length; i++) {
        tree.create_node(parent, node[i], "last", function(){});
        parent = node[i].id;
    }
}

I found this issue: https://github.com/vakata/jstree/issues/2055 where you said that a parent node should be loaded and opened before a child can be added to it. But I just can't seem to figure out what I need to add the above code to make it work. I tried a lot of different combinations of calling load_node and open_node, but most of the time this just destroys the branch or the tree completely (meaning you can't click on nodes or expand it).

I also tried a recursive approach using the callback you provided, something like (pseudocode):

function createBranch(parentId, node) {
    tree.create_node(parent, node, "last", function() {
        if (node.child) {
            createBranch(node.id, node.child);
        }
    });   
}

this also has not the desired effect.

No matter what i try, it seems the issue for me is that I cannot create child nodes for nodes which have been created by create_node. Thank you !

vakata commented 1 year ago

A basic example seems to work fine for me: https://jsfiddle.net/Lf8bpcnx/

Maybe there is something more specific in you setup? If you are creating in an unloaded node this will mean create node will be async (the original parent node will have to be loaded and then create node will be executed again).