Check and uncheck nodes in treepanel

Dev picture Dev · Jan 24, 2014 · Viewed 8k times · Source

To do : Checking a child should check parents up the tree, and clearing a parent checkbox should clear all of it's children.

Example :

parent1
--child1
--child2
     --subChild1
     --subChild2

Scenario1 :In above case, if subChild1 is checked,then parent1 and child2 should also be checked.

Scenario2 :If parent1 is checked,then all its children(checked) should be unchecked.

From this Check/Uncheck Nodes,only when a parent node is selected its child node are getting selected.

Their is something implemeted the way I want,but not able to figure it out(implementation) as the complexity level is high enough for me to understand.Here is it reference

Please help me resolve this functionality.Thanks.Any help would be appreciated.

Answer

Dev picture Dev · Jan 24, 2014

Here is what I have done.It took some time but it worked.

checkchange : function(node, checked, opts) {

 function clearNodeSelection(node){
  //node is not leaf node
   console.log(node);
   leafNode = node.raw.leaf;
   if(!leafNode){
      node.cascadeBy(function(node) {
            node.set('checked', false);
       })
   }
 }

 if(!checked){
    console.log("inside !checked : "+checked);
    clearNodeSelection(node);
 }

 function selectParentNodes(node){
     var parentNode = node.parentNode;
     if(parentNode){
        parentNode.set('checked', true);
        selectParentNodes(parentNode);
    }
 }

 selectParentNodes(node);
}

Atlast,Thanks @VDP. 'SO' is awesome.