我正在尝试使用 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/