jstree delete_node() is not deleting

心已入冬 提交于 2019-12-08 17:44:06

问题


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.


回答1:


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.




回答2:


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.
        }



回答3:


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

}



回答4:


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



来源:https://stackoverflow.com/questions/31715584/jstree-delete-node-is-not-deleting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!