Extjs TreePanel 完美实现

来源:互联网 发布:uniprotRef数据库 编辑:程序博客网 时间:2024/05/06 02:50

treepanel.on('change',function(node,checked){

    treecheck(node,checked);//调用下列方法

});

 

////树形控件的选择
 var treecheck=function(node,checked){
     node.expand();
     if(node.hasChildNodes()){
       ChildWithParent(node,checked);
     }
     //如果是选中状态,则选中所有父节点
     if(checked){
       ParentWithChild1(node,checked);   
     }
     //如果不选中
     else{
        ParentWithChild2(node,checked);
     }
 }
 //选择结点,影响所有子节点
 var ChildWithParent=function(node,bool){
  if(node){
   node.attributes.checked = bool;
   if(node.hasChildNodes()){
    node.eachChild(function(child){
     child.ui.toggleCheck(bool);
     child.attributes.checked = bool;
     ChildWithParent(child,bool);
    });
   }
  }
 }
 //如果是选中状态,则选中父节点---递归
 var ParentWithChild1=function(node,bool){   
  if(node.parentNode.getUI().checkbox){
   var parent=node.parentNode;
   parent.attributes.checked=bool;
   parent.getUI().checkbox.checked=bool;
   ParentWithChild1(parent,bool);
  }
 }
 //如果是不选中状态,则遍历父节点,如果该父节点下的所有节点都为不选中,则取消选中
 var ParentWithChild2=function(node,bool){
  if(node.parentNode.getUI().checkbox){
   var parent=node.parentNode;    
   var temp=false;
   parent.eachChild(function(child){
    if(child.getUI().checkbox.checked==true){
     temp=true;
     return false;
    }    
   });   
   if(!temp){
    parent.attributes.checked=bool;
    parent.getUI().checkbox.checked=bool;
    temp=false;
    ParentWithChild2(parent,bool);
   }
  }
 }

原创粉丝点击