I am using jQuery dynaTree in my application and I want to programmatically select all child nodes when the 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 I want that when I click on a node tree with the heading "package 1", all of 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).
The following is the approach I 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 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) state, I get all the child nodes of the element that the user selected, and it gives me the correct value, which I can see from the alert (child.length) statement. Then I run the loop to select all the children, but the loop never goes beyond the expression var x = child[i].select(true);
And I can never see the alert(i) statement executing. The result of the above statement is that if I select package 1, module 1.1 and document 1.1.1 are also selected, but it never executes the alert(i) operator - no other children of package 1 were selected. In my opinion, when the first child[i].select(true) statement is executed, it also fires an on select event for its children, thereby doing a recursion of the type
I think this is right? Regardless of what the recursion is or what it does, it does not end the loop and executes the following alert(i) command.
Please help me in solving this problem. I am dying to see this warning, any suggestion and help is greatly appreciated.