Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing the newly created node with JSTree contextmenu

I'm trying to capture the name of the newly created node with jstree's contextmenu. I can capture the name of the parent node that I'm adding the new node under (with obj.text()), however, what I really need is the name of the newly created node.

So, somehow, there needs to be an "onChange" event that can be called within jstree contextmenu that fires once the user hits enter on that newly created node?

Any ideas? I've enclosed the contextmenu code:

}).jstree({
        json_data: {
            data: RBSTreeModel,
            ajax: {
                type: "POST",
                data: function (n) {
                    return {
                        NodeID: n.attr("id").substring(4),
                        Level: n.attr("name").substring(7)
                    };
                },
                url: function (node) {
                    return "/Audit/GetRequirementsTreeStructure";
                },
                success: function (new_data) {
                    return new_data;
                }
            }
        },
        contextmenu: {
            items: function($node) {
                return {
                    createItem : {
                        "label" : "Create New Branch",
                        "action" : function(obj) { this.create(obj); alert(obj.text())},
                        "_class" : "class"
                    },
                    renameItem : {
                        "label" : "Rename Branch",
                        "action" : function(obj) { this.rename(obj);}
                    },
                    deleteItem : {
                        "label" : "Remove Branch",
                        "action" : function(obj) { this.remove(obj); }
                    }
                };
            }
        },
        plugins: ["themes", "json_data", "ui", "crrm", "contextmenu"]
    });
like image 293
TheDude Avatar asked Jan 16 '23 15:01

TheDude


2 Answers

You can bind to the "create.jstree" event, which will fire after a node is created. In the callback of that event, you will have access to the newly created node and can rollback/revert the create node action if you choose. The documentation for it is lacking, but there is an example on the demo page. Here is another example that came from my code:

}).jstree({... You jstree setup code...})
        .bind("create.jstree", function(e, data) {
            // use your dev tools to examine the data object
            // It is packed with lots of useful info
            // data.rslt is your new node
            if (data.rslt.parent == -1) {
                alert("Can not create new root directory");
                // Rollback/delete the newly created node
                $.jstree.rollback(data.rlbk);
                return;
            }
            if (!FileNameIsValid(data.rslt.name)) {
                alert("Invalid file name");
                // Rollback/delete the newly created node
                $.jstree.rollback(data.rlbk);
                return;
            }
            .. Your code etc...
        })
like image 83
Bojin Li Avatar answered Jan 23 '23 14:01

Bojin Li


Based on Bojin Li's answer, it seems that the latest version of jsTree uses the event "create_node" instead of "create":


}).jstree({... You jstree setup code...})
      .bind("create_node.jstree", function(e, data) {
        ...
       });

like image 22
Astrip Avatar answered Jan 23 '23 13:01

Astrip