TreePanel checkbox 联动

经典的checkbox树默认是不会有联动处理的。这里变量INDEX_CATEGORY_CHECKING是为了避免各个节点事件触发后的递归调用,从而解决了过多递归的问题。

代码
'checkchange'function(node, checked){

    
if (!INDEX_CATEGORY_CHECKING) {
        INDEX_CATEGORY_CHECKING 
= true;

        
// true时,子同父,父也同子
        if (checked == true) {
            node.attributes.checked 
= true;

            
// 父变为true,所有子都跟着变化
            (function(node) {
                
var _this = arguments.callee;
                
if (!node.isLeaf()) {
                    node.expand();
                    node.eachChild(
function(child) {
                        child.ui.toggleCheck(
true);
                        child.attributes.checked 
= true;
                        _this.call(_this, child);
                    });
                }
             })(node);

            
// 父变为true,父的父(如果有的话)也应该都为true
            (function(node) {
                
var _this = arguments.callee;
                
if (node.parentNode && node.parentNode != t.root) {
                    
var pnode = node.parentNode;
                    pnode.ui.toggleCheck(
true);
                    pnode.attributes.checked 
= true;
                    _this.call(_this, pnode);
                }
             })(node);

        } 
else { // false 时,子同父,但父不一定同子
            node.attributes.checked = false;

            
// 父变为false,所有子跟着变化
            (function(node) {
                
var _this = arguments.callee;
                
if (!node.isLeaf()) {
                    node.expand();
                    node.eachChild(
function(child) {
                        child.ui.toggleCheck(
false);
                        child.attributes.checked 
= false;
                        _this.call(_this, child);
                    });
                }
             })(node);

            
// 父变为false,但父的父(如果有的话)不一定变化
            (function(node) {
                
var _this = arguments.callee;
                
if (node.parentNode && node.parentNode != t.root) {
                    
var pnode = node.parentNode;

                    
var chk = false;
                    pnode.eachChild(
function(child) {
                        
if (child.attributes.checked == true) {
                            chk 
= true;
                            
return false;
                        }
                    });
                    
if (chk == true) {
                        
return;
                    } 
else {
                        pnode.ui.toggleCheck(
false);
                        pnode.attributes.checked 
= false;
                        _this.call(_this, pnode);
                    }
                }
             })(node);
        }

        INDEX_CATEGORY_CHECKING 
= false;
    }
}
 
原文地址:https://www.cnblogs.com/KingStar/p/1755299.html