Search code examples
jqueryajaxtreenestedjstree

jstree : how to undelete / restore a node


I am using jsTree in my application with contextmenu plugin.

When deleting a node, I have an ajax request that performs the operation on my database.

I'd like to actually delete the node from the view only if the ajax returns a operation = true parameter.

How can I undelete a jstree node that has been deleted with the following code :

function (data) {
    var inst = $.jstree.reference(data.reference),
        obj = inst.get_node(data.reference);
    if(inst.is_selected(obj)) {
        inst.delete_node(inst.get_selected());
    }
    else {
        inst.delete_node(obj);
    }
}

Solution

  • I only the delete the node from the tree when the call was successful. It doesn't make sense to the delete the node first, then make the call and if it fails then try to re-add the node. Here is an example to only delete the node if the ajax call was a success:

    var node = $('#tree').jstree('get_selected');
    
    $.ajax({
        url: link,
        type: 'GET',
        data: { id: node.attr('id') },
        async: false,
        success: function (data, text) {
            $('#tree').jstree('delete_node', node);
        },
        error: function (request, status, error) {
        }
    });
    

    ps. this will get the current selected node