Javascript 如何重命名 jsTree 节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6254735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I rename a jsTree node
提问by Chris
I am not talking about $("#demo1").jstree("rename",node)
which makes the node editable for the user. I am talking about the name being changed within the code. For example my nodes are all prefixed with a 2 digit number "[01]" so before I call $("#demo1").jstree("rename",node)
I want to strip out the prefix, and put it back in once the user has finished editing. I have tried selecting "#nodeid a" but inside the hyperlink there is an ins tag and this gets replaced if i replace the URL contents. The documentation hasn't been helpful and I havent had much luck looking through the libraries code, can any help me? Chris
我不是在谈论$("#demo1").jstree("rename",node)
哪个使用户可以编辑节点。我说的是在代码中更改的名称。例如,我的节点都以 2 位数字“[01]”$("#demo1").jstree("rename",node)
为前缀,因此在我打电话之前,我想去掉前缀,并在用户完成编辑后将其放回原处。我试过选择“#nodeid a”,但在超链接中有一个 ins 标签,如果我替换 URL 内容,它会被替换。该文档没有帮助,而且我没有太多运气浏览库代码,可以帮助我吗?克里斯
回答by Arend
The recommendedmethod is to use rename_node
该建议的方法是使用rename_node
$("#demo1").jstree('rename_node', node , text );
Please keep in mindthat by default all modifications to the tree are prevented(create, rename, move, delete). To enable them set
core.check_callback
to true
请记住,默认情况下会阻止对树的所有修改(创建、重命名、移动、删除)。启用它们设置
core.check_callback
为 true
$('#demo1').jstree({
'core': {
'check_callback': true,
/// rest of the options...
}
});
Rename your node (alternative, not recommended)
重命名您的节点(替代,不推荐)
$("#demo1").jstree('set_text', node , text );
Debugging
调试
If you still encounter trouble, you can use this method to get the last error.
如果还是遇到问题,可以用这个方法获取最后一个错误。
$('#demo1').jstree(true).last_error()
For older versions (v1.*)
对于旧版本 (v1.*)
$("#demo1").jstree('rename_node', [node , text] );
$("#demo1").jstree('set_text', [node , text] );
See also:
也可以看看:
- this jsfiddlefor the comparison and example of both methods.
- Interaction with jsTree(how to call API methods)
- API documentation of
rename_node
- API documentation of
set_text
- 这个 jsfiddle用于比较和示例这两种方法。
- 与jsTree的交互(如何调用API方法)
- API 文档
rename_node
- API 文档
set_text
回答by Mohit Arvind khakharia
I believe there is an syntax error with respect to the square braces "[" in the above answer. I use jsTree 3.0.4 and this is the correct syntax -
我相信上述答案中的方括号“[”存在语法错误。我使用 jsTree 3.0.4,这是正确的语法 -
right - $("#demo1").jstree('set_text',node,text);
wrong - $("#demo1").jstree('rename_node', [node , text] );
Example -
$("#tree_3").jstree('set_text',"#idSelectorForNode" ,"NewName");
回答by Cherry
You should turn on the switch to allow the rename operation, such as:
您应该打开开关以允许重命名操作,例如:
$('#container').jstree({
'core' : {
'check_callback' : function (operation, node, node_parent, node_position, more) {
// operation can be 'create_node', 'rename_node', 'delete_node', 'move_node' or 'copy_node'
// in case of 'rename_node' node_position is filled with the new node name
return operation === 'rename_node' ? true : false;
}
});