taki Martillo
taki Martillo

Reputation: 406

jstree delete_node() is not deleting

I've been cobbling together a function to put together a custom context menu for different nodes. Well, so far so good on getting different label to show up for clicks on folders or files, but not so much on actually deleting them.

Have a look. I had to ... do a little bit of a hacky workaround because I couldn't get the node.hasClass('jstree-open') yada yada to work right, but this is generally working up to the bit that's supposed to do the deleting

function customMenu(node) {
            //Show a different label for renaming files and folders
            var ID = $(node).attr('id');
            if (ID == "j1_1") {
                return items = {}; //no context menu for the root
            }
            var $mynode = $('#' + ID);
            var renameLabel;
            var deleteLabel;
            var folder = false;
            if ($mynode.hasClass("jstree-closed") || $mynode.hasClass("jstree-open")) { //If node is a folder
                renameLabel = "Rename Folder";
                deleteLabel = "Delete Folder";
                folder = true;
            }
            else {
                renameLabel = "Rename File";
                deleteLabel = "Delete File";
            }
            var items = {
                "rename" : {
                    "label" : renameLabel, 
                    "action": function (obj) {
                         //nothing here yet.
                    }
                },
                "delete" : {
                    "label" : deleteLabel,
                    "action": function (obj) {
                        //tree.delete_node($(node));
                        //this.remove(obj);
                        //$('#treeView').jstree('remove', $(node));
                        //nothing is working.
                    }
                }
            };

            return items;
        }

I've put together a fiddle for your convenience: http://jsfiddle.net/dpzy8xjb/ I don't think it really needs to be said that I'm not super experienced with jQuery or dealing with third party APIs, so... Be gentle.

Upvotes: 7

Views: 11260

Answers (4)

Bogdan Ionescu
Bogdan Ionescu

Reputation: 11

The nodes cannot be deleted unless core.check_callback is set to true.

Upvotes: 1

Agu Dondo
Agu Dondo

Reputation: 13569

I had this problem, and none of the solutions worked. And as the documentation says:

all modifications to the tree are prevented (create, rename, move, delete). To enable them set core.check_callback to true.

In my case, I had a check_callback function (I'm using drag and drop) that was returning false when deleting a node.

I've adjusted it to 'delete_node' like this:

check_callback: function(operation, node, parent, position){
        if(operation == 'delete_node'){
            return true;
        }
        // ... rest of the code

}

Upvotes: 2

Janty
Janty

Reputation: 1714

DO use tree.delete_node([node]); for delete.

Updated Fiddle

Edit:

The code you did is same as the node.

        var ID = $(node).attr('id');
        var $mynode = $('#' + ID);

Its the same object node.

Upvotes: 2

taki Martillo
taki Martillo

Reputation: 406

I swear to god there is nothing that drives me to figure out a problem faster than posting it on StackOverflow.

Fixed:

       "delete": {
            "label": deleteLabel,
                "action": function (obj) {
                //tree.delete_node($(node));
                tree.delete_node($mynode); //<<--works.
        }

Upvotes: 1

Related Questions