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

标签 javascript events event-handling contextmenu jstree

我正在尝试使用 jstree 的上下文菜单捕获新创建的节点的名称。我可以捕获我在其下添加新节点的父节点的名称(使用 obj.text()),但是,我真正需要的是新创建的节点的名称。

因此,不知何故,需要有一个可以在 jstree contextmenu 中调用的“onChange”事件,一旦用户在新创建的节点上按下 enter,该事件就会触发?

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

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

最佳答案

您可以绑定(bind)到“create.jstree”事件,该事件将在创建节点后触发。在该事件的回调中,您将有权访问新创建的节点,并且可以根据需要回滚/恢复创建节点的操作。缺少它的文档,但在 demo page 上有一个示例.这是来 self 的代码的另一个示例:

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

关于javascript - 使用 JSTree 上下文菜单捕获新创建的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12269884/

相关文章:

javascript - css 不是 'removing' 文本颜色设置为透明

ios - iOS 上带有 eventWithIdentifier 的 EKEvent

c# - SelectedIndexChanges 在 TextChanged 事件之前触发

c# - 这是通用委托(delegate)和事件的正确语法吗?

mysql事件不起作用

java - 无法理解和解决此程序的方法覆盖错误

c++ - Boost::带有共享指针的侵入式列表

javascript - 当我在不同的文件中提供服务时,Angular 会抛出错误

javascript - 将不同宽高比的图像均匀地放入多行

php - 在表单验证期间设置选择元素值的替代方法?