最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I select child node programmatically, in dynatree? - Stack Overflow

programmeradmin2浏览0评论

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 plete 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.

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 plete 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.

Share Improve this question edited Feb 17, 2017 at 14:10 AncientSwordRage 7,64521 gold badges99 silver badges193 bronze badges asked May 13, 2010 at 13:12 Muhammad Adeel ZahidMuhammad Adeel Zahid 17.8k16 gold badges94 silver badges159 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Barely tested, but you could try something like this:

$(function(){
    var inEventHandler = false;
    $("#tree").dynatree({
        checkbox: true,
        selectMode: 2,
        [...]
        onSelect: function(select, dtnode) {
            // Ignore, if this is a recursive call
            if(inEventHandler) 
                return;
            // Select all children of currently selected node
            try {
                inEventHandler = true;
                dtnode.visit(function(childNode){
                    childNode.select(true);
                });
            } finally {
                inEventHandler = false;
            }
        }

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:

edibles
edibles > fruits
edibles > fruits > apples
edibles > fruits > apples > green apples
edibles > fruits > apples > green apples > granny smith
edible > vegetables
edible > vegetables > potatoes

whilst clicking on "green apples" would output:

edibles > fruits > apples > green apples
edibles > fruits > apples > green apples > granny smith

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.

Use the following function in place of the one from mar10's answer.

All child nodes are selected/unselected on parent node select/unselect.

onSelect: function (isSelected, node) {
    node.visit(function (childNode) {
        childNode.select(isSelected);
    });
},
发布评论

评论列表(0)

  1. 暂无评论