如何在动态树中以编程方式选择子节点?
我在应用程序中使用 jQuery 的 dynaTree,并且希望在选择父节点时以编程方式选择所有子节点。我的树的结构如下
<div id = "tree">
<ul>
<li>package 1
<ul>
<li>module 1.1
<ul>
<li> document 1.1.1</li>
<li> document 1.1.2</li>
</ul>
</li>
<li>module 1.2
<ul>
<li>document 1.2.1</li>
<li>document 1.2.2</li>
</ul>
</li>
</ul>
</li>
<li> package 2
<ul>
<li> module 2.1
<ul>
<li>document 2.1.1</li>
<li>document 2.1.1</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
现在我想要的是,当我单击标题为“package 1”的树节点时,它的所有子节点即(模块1.1,文档1.1.1,文档1.1.2,模块1.2,文档1.2 .1,文件 1.2.2) 也应选择。
以下是我尝试使用的方法:
$("#tree").dynatree({
onSelect: function(flag, dtnode) {
// This will happen each time a check box is selected/deselected
var selectedNodes = dtnode.tree.getSelectedNodes();
var selectedKeys = $.map(selectedNodes, function(node) {
//alert(node.data.key);
return node.data.key;
});
// Set the hidden input field's value to the selected items
$('#SelectedItems').val(selectedKeys.join(","));
if (flag) {
child = dtnode.childList;
alert(child.length);
for (i = 0; i < child.length; i++) {
var x = child[i].select(true);
alert(i);
}
}
},
checkbox: true,
onActivate: function(dtnode) {
//alert("You activated " + dtnode.data.key);
}
});
在 if(flag)
条件中,我获取用户选择的元素的所有子节点,它为我提供了可以从警报中看到的正确值(child.length) 语句。然后我运行循环来选择所有子项,但循环永远不会超出语句 var x = child[i].select(true);
并且我永远看不到语句 alert(i )
正在执行。上述语句的结果是,如果我选择包 1,则也会选择模块 1.1 和文档 1.1.1,但它永远不会执行 alert(i)
语句 - 没有选择包 1 的其他子项。在我看来,当第一次执行 child[i].select(true)
语句时,它也会触发其子级的 on select 事件,从而进行递归之类的事情
我的想法正确吗?无论它执行什么递归或执行什么操作,它都不会完成循环并执行下一条指令 alert(i)
。
请帮助我解决这个问题。我非常希望看到这种警报,任何建议和帮助都将受到高度赞赏。
I'm using jQuery's dynaTree in my application and I want to select the all the child nodes programmatically when a parent node is selected. The structure of my tree is as follows
<div id = "tree">
<ul>
<li>package 1
<ul>
<li>module 1.1
<ul>
<li> document 1.1.1</li>
<li> document 1.1.2</li>
</ul>
</li>
<li>module 1.2
<ul>
<li>document 1.2.1</li>
<li>document 1.2.2</li>
</ul>
</li>
</ul>
</li>
<li> package 2
<ul>
<li> module 2.1
<ul>
<li>document 2.1.1</li>
<li>document 2.1.1</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
Now what I want is that when I click on tree node with title "package 1" all its child nodes i.e (module 1.1, document 1.1.1, document 1.1.2, module 1.2, document 1.2.1, document 1.2.2) should also be selected.
Below is the approach I have tried to use:
$("#tree").dynatree({
onSelect: function(flag, dtnode) {
// This will happen each time a check box is selected/deselected
var selectedNodes = dtnode.tree.getSelectedNodes();
var selectedKeys = $.map(selectedNodes, function(node) {
//alert(node.data.key);
return node.data.key;
});
// Set the hidden input field's value to the selected items
$('#SelectedItems').val(selectedKeys.join(","));
if (flag) {
child = dtnode.childList;
alert(child.length);
for (i = 0; i < child.length; i++) {
var x = child[i].select(true);
alert(i);
}
}
},
checkbox: true,
onActivate: function(dtnode) {
//alert("You activated " + dtnode.data.key);
}
});
In the if(flag)
condition I get all the child nodes of element that is selected by user and it gives me the correct value that I can see from alert(child.length) statement. Then I run the loop to select all the children but loop never goes beyond the statement var x = child[i].select(true);
And I can never see the statement alert(i)
being executed. The result of above statement is that if I select package 1, module 1.1 and document 1.1.1 is also selected but it never executes the alert(i)
statement - no other children of package 1 are selected. In my view when first time child[i].select(true)
statement is executed it also triggers the on select event of its children thus making a recursion kind of thing
Is my thinking correct? No matter what recursion or what on earth it does, it does not complete the loop and execute the very next instruction alert(i)
.
Please help me in solving this problem. I'm dying to see that alert, any suggestion and help is highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
几乎没有测试过,但你可以尝试这样的事情:
Barely tested, but you could try something like this:
虽然我不是 Dynatree 内部功能的专家,但我编写了一些代码,为单击的节点及其所有后代生成面包屑。
在下面的示例中,单击“edibles”将输出:
同时单击“green apples”将输出:
您可以从此示例中汲取灵感,从所有子文档中检索所需的字段并生成 HTML 输出。
代码在此线程中:
从具有 3 列(id、路径、名称)的 PHP 数组生成文本面包屑的最佳方法?
PHP 脚本本身是通过 jQuery.ajax({.. .}); 查询。
Although being not an expert of Dynatree's internal capacities, I wrote some code that generates breadcrumbs for the clicked node and all its descendants.
In example below, clicking on "edibles" would output:
whilst clicking on "green apples" would output:
You can draw inspiration from this example and retrieve the fields you need from all children documents and generate your HTML output.
The code is in this thread:
Best way to generate text breadcrumbs from a PHP array having 3 columns (id, path, name)?
The PHP script itself is called from Dynatree's onActivate event handler through a
jQuery.ajax({...});
query.使用以下函数代替 mar10 答案中的函数。
所有子节点都在父节点选择/取消选择时选择/取消选择。
Use the following function in place of the one from mar10's answer.
All child nodes are selected/unselected on parent node select/unselect.