我创建了一个类似
的jsTree$('.containerTree').jstree({
"core": {
"check_callback": true,
"data": ["Root"]
}
});
添加脚本:
<link rel="stylesheet" href="~/Content/bootstrap.css">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/jstree/3.0.1/themes/default/style.min.css" />
<script src="~/Scripts/jquery-3.1.0.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jstree/3.0.9/jstree.min.js"></script>
所以,我想创建并显示带有子节点的根节点列表。为此我在循环中使用“create_node”函数:
for (var iteration = 0; iteration < 10; iteration++) {
var newNodes= $(".containerTree").jstree("create_node",null,"root"+iteration, "last", function (node) {
for (var i = 0; i < 10; i++) {
var newDataChild = {
"text": "child" + i,
"id": "child" + i
};
tree.create_node(node, newDataChild, "last", function (newChild) {
});
}
});
我希望看到这样的结果:
Root1
----child1
...
RootN
----child1
..........
结果创建了节点,但仅显示最后一个根节点。谁能解释为什么会这样?
答案 0 :(得分:0)
您需要解决两个问题:
tree.create_node(...
- 树变量未在任何地方定义.create_node
- 更好地使用与第一次相同的方法所以正确的代码如下所示。查看演示 - demo Fiddle
var $tree = $(".containerTree");
for (var iteration = 0; iteration < 10; iteration++) {
$tree.jstree("create_node",null,"root"+iteration, "last", function (node) {
for (var i = 0; i < 10; i++) {
var newDataChild = {
"text": "child" + i,
"id": "child" + i
};
$tree.jstree("create_node", node, newDataChild, "last");
}
});
}