以编程方式将子节点添加到jstree [英] Programmatically adding child nodes to a jstree
问题描述
我正在尝试编写一些将节点动态添加到jstree的代码.我已经按照 http://www.jstree.com/documentation/crrm 上的文档进行操作,但是无法获得一个简单的示例来工作-正在添加节点child2,但是将其添加到节点'root.id'而不是指定的'child1.id'...任何提示将不胜感激.代码如下
I'm trying to write some code that adds nodes to a jstree dynamically. I've followed the doc at http://www.jstree.com/documentation/crrm but can't get a simple example to work -- the node child2 is being added, but it is being added to the node 'root.id' rather than 'child1.id' as specified... Any tips would be much appreciated. Code follows
<html>
<head>
<script type="text/javascript" src="http://static.jstree.com/v.1.0rc2/jquery.js"></script>
<script type="text/javascript" src="http://static.jstree.com/v.1.0rc2/jquery.jstree.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(function () {
$("#tree").jstree({
"json_data" : {
"data" : [
{
"data" : "parent",
"attr" : { "id" : "root.id" },
"children" : [ { "data" : "child1",
"attr" : { "id" : "child1.id" },
"children" : [ ] }
]
},
]
},
"plugins" : [ "themes", "json_data", "crrm" ]
});
});
$("#add").click(function() {
$("#tree").jstree("create", $("#child1.id"), "inside", { "data" : "child2" },
function() { alert("added"); }, true);
});
});
</script>
</head>
<body>
<div id="tree" name="tree"></div>
<input type="button" id="add" value="add" />
</body>
</html>
推荐答案
在ID中使用句点时,您需要像这样对它们进行转义:
When using periods in ID's you need to escape them like so:
$("#tree").jstree("create", $("#child1\\.id"), "inside", { "data" : "child2" },
function() { alert("added"); }, true);
这是因为它使用jQuery选择器的方式.此处的jsTree常见问题解答中提到了它: http://www.jstree.com/faq/
This is because of how it uses jQuery selectors. It is mentioned in the jsTree FAQ located here: http://www.jstree.com/faq/
这篇关于以编程方式将子节点添加到jstree的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!