Oliver G.
Oliver G.

Reputation: 227

replace a single node with a subtree in jstree

How can I replace in a jstree a child node with a subtree (containing other child nodes)?

My root node db contains a childnode cfg. If I select that cfg node I want to replace it with the subtree loaded on demand.

getJson on https://192.168.1.1/db/ gives me this json object:

{
"class": "Container",
"name": "db",
"nb": 6,
"objects": [{
    "name": "alarms"
}, {
    "name": "cfg"
}, {
    "name": "commands"
}, {
    "name": "csrs"
}, {
    "name": "downloads"
}, {
    "name": "stats"
}],
"pub": false}

getJson on https://192.168.1.1/db/cfg/ gives me this json object:

{
"class": "Container",
"name": "cfg",
"nb": 6,
"objects": [{
    "name": "device"
}, {
    "name": "dm"
}, {
    "name": "flows"
}, {
    "name": "ppp"
}, {
    "name": "properties"
}, {
    "name": "services"
}],
"pub": false

}

I already tried something like this:

.bind("select_node.jstree",  function (evt, data) {
    if(data.event) {

    var node = $('#jstree_demo').jstree(true).get_node(data.node.id)

    $.getJSON('https://192.168.1.1/db/' + data.node.text, function(jsondata) {

    var myJson = replace_inline(jsondata)

    $('#jstree_demo').jstree(true).settings.core.data = myJson;
    $('#jstree_demo').jstree(true).refresh(true);
    });
}

but here the hole tree get replaced by my subtree.

Upvotes: 2

Views: 1024

Answers (1)

Oliver G.
Oliver G.

Reputation: 227

My Solution is to create a new node append it on the grandparent node and delete the parent node. I also enablied the sort plugin of jstree. So that the Subtree has the postion of the deleted one.

    .bind("select_node.jstree",  function (evt, data) {
    if(data.event) {

    var node = $('#jstree_demo').jstree(true).get_node(data.node.id)

    $.getJSON('https://192.168.1.1/db/' + data.node.text, function(jsondata) {

    var myJson = replace_inline(jsondata)

    var position = 'inside';
    var parent = $('#jstree_demo').jstree('get_selected');
    var grandparent= data.instance.get_node(data.node.parent)

    var newNode = { state: "open", myJson };

    $('#jstree_demo').jstree().create_node(grandparent , myJson, "last", 
                                function(){                                     
                                            //alert("create node done");
                                        });
    $("#jstree_demo").jstree(true).delete_node(node);
    });
}

Upvotes: 1

Related Questions