天天看点

转:Extjs选中当前节点后自动选中子节点和父节点

//根据当前节点选中父节点及其子节点 
    tree.on('checkchange', function(node, flag) { 
//获取所有子节点 
node.cascade( function( node ){ 
   node.attributes.checked = flag; 
   node.ui.checkbox.checked = flag; 
   return true; 
    }); 
   //获取所有父节点 
      var pNode = node.parentNode; 
      for(; pNode.id !="root"; pNode = pNode.parentNode ){ 
       if (flag || tree.getChecked(id, node.parentNode) == "") { 
            pNode.ui.checkbox.checked = flag; 
            pNode.attributes.checked = flag; 
      }   
      } 
}); 
           

方法2(参考)

//	    var childbool=true;
//	    tree1.on({   
//	    	  
//	        'checkchange':function(node,checked){   
//	  
//	  
//	  
//	            var parentNode=node.parentNode;   
//	  
//	                   
//	  
//	            if(checked){   
//	  
//	                /**  
//	 
//	                 * 节点为真时,此节点的子节点,判断此节点的父节点时,判断父节点的子节点是否全部为  
//	 
//	                 * 真,如果全部为真,则此父节点为真,如果不为真则不变 全部为真  
//	 
//	                 */ 
//	                 
//	                if(childbool){  
//	                
//	                        var childNodes=node.childNodes;   
//	                           
//	          
//	                        for(var i=0;i<childNodes.length;i++){   
//	          
//	                            var childNode=childNodes[i];   
//	          
//	                            if(!childNode.attributes.checked){   
//	          
//	                                childNode.ui.toggleCheck();   
//	          
//	                            }   
//	          
//	                        }   
//	                 }
//	                 
//	                /**  
//	 
//	                 * 此如果此节点又父节点,则判断此父节点的子节点是否全为真 如果全为真则此父节点也为真  
//	 
//	                 */        
//	  
//	                if(parentNode&&!parentNode.attributes.checked){  
//	                     
//	                     childbool=false;
//	                      
//	                     parentNode.ui.toggleCheck();                    
//	                     
//	  
//	                }
//	                else  childbool=true;
//	                   
//	  
//	            }else{   
//	  
//	                      
//	                
//	                var childNodes=node.childNodes;   
//	  
//	                for(var i=0;i<childNodes.length;i++){   
//	  
//	                    var childNode=childNodes[i];   
//	  
//	                    if(childNode.attributes.checked){   
//	  
//	                        childNode.ui.toggleCheck();   
//	  
//	                    }   
//	  
//	                }      
//	  
//	            }   
//	  
//	           
//	  
//	        }   
//	  
//	    });      
//	  



//	function ParentState(parentNode)
//	{
//	   
//	    var brothNodes=null;
//	    
//	    if(parentNode!=null) //兄弟接点
//	                       brothNodes=parentNode.childNodes;
//	    else return false;
//	                 
//	    var brothflag=0;                 
//	                
//	    for(var i=0;i<brothNodes.length;i++){   
//	  
//	    var brothNode=brothNodes[i];   
//	  
//	    if(brothNode.attributes.checked){   
//	  
//	                          break;   
//	  
//	                    }
//	                    else   brothflag++; 
//	  
//	    }           
//	    if(brothflag==brothNodes.length){ //说明兄弟节点没选种的
//	                if(parentNode.attributes.checked)
//	                               parentNode.ui.toggleCheck();
//	                ParentState(parentNode.parentNode);
//	    }
//	}  
//	    
//