Treeview 父子节点选中的级联操作

      选中父节点,同时选中子节点。 选中子节点,自动勾选上父节点,当所有的子节点都没有选中的时候,又自动取消父节点的勾选。

 

代码
    < script src = " http://www.cnblogs.com/js/jquery-1.4.2.js "  type = " text/javascript " >< / script>
     < script language = " javascript "  type = " text/javascript " >
        $(document).ready(Bind);
        
function  Bind() {
            BindTreeView(
" tvMenu " true true );
        }
        
function  BindTreeView(treeViewId,selectChild,selectParent) {

            $(
" # "   +  treeViewId  +   "  input " ).click( function () {
                
if  (selectChild) {
                    
var  divItem  =  $( this ).parent().parent().parent().parent().next();
                    
if  (divItem[ 0 !=   null ){  //  屏蔽选最后一个时,会弹出错误的漏洞
                         if  (divItem[ 0 ].tagName  ==   " DIV " )
                            divItem.find(
" input " ).attr( " checked " , $( this ).get( 0 ).checked); 
                    }
                }
                
if  (selectParent) {
                    SelectParentNode(treeViewId, $(
this ));
                }
            });
        }

        
function  SelectParentNode(treeViewId,node) {
            
if  (node.get( 0 !=   null  ){
                
if  (node.get( 0 ).checked  ==   true ) {
                    
var  pdiv  =  node.parent().parent().parent().parent().parent();
                    
if  (pdiv.get( 0 ).id  !=  treeViewId) {
                        
                        pdiv.prev().find(
" input " ).attr( " checked " , node.get( 0 ).checked);

                        SelectParentNode(treeViewId, pdiv.prev().find(
" input " ));
                    }
                } 
else  {  //  取消所有子的选择,父节点也取消
                     var  pdiv  =  node.parent().parent().parent().parent().parent();
                    
if  (pdiv.get( 0 ).id  !=  treeViewId) {

                        
var  divItem  =  node.parent().parent().parent().parent().parent();
                        
if  (divItem[ 0 !=   null ) {
                            
if  (divItem[ 0 ].tagName  ==   " DIV " ) {
                                
if  (divItem.find( " input[type='checkbox']:checked " ).size()  ==   0  ) {
                                    pdiv.prev().find(
" input " ).attr( " checked " false );
                                }
                            }
                        }

                        SelectParentNode(treeViewId, pdiv.prev().find(
" input " ));
                    }
                }
            }
        }
    
< / script>

 

 

你可能感兴趣的:(treeview)