使用JSTree上下文菜单捕获新创建的节点

Capturing the newly created node with JSTree contextmenu

本文关键字:创建 节点 新创建 上下文 JSTree 菜单 使用      更新时间:2023-09-26

我试图用jstree的contextmenu捕获新创建的节点的名称。我可以捕获添加新节点的父节点的名称(使用obj.text()),但是,我真正需要的是新建节点的名称。

因此,不知何故,需要在jstree上下文菜单中调用一个"onChange"事件,一旦用户在新创建的节点上点击回车键,就会触发该事件?

有什么想法吗?我附上了上下文菜单代码:

}).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"]
    });

您可以绑定到"create.jstree"事件,该事件将在创建节点后激发。在该事件的回调中,您将可以访问新创建的节点,并且如果您选择,可以回滚/还原创建节点操作。虽然缺少相关文档,但演示页面上有一个示例。下面是我的代码中的另一个例子:

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

根据李的回答,最新版本的jsTree似乎使用了事件"create_node"而不是"create":

}).jstree({…您的jstree设置代码…}).bind("create_node.jstree",函数(e,数据){。。。});