TreePanel checkbox 联动

本文介绍了一个实现Checkbox树联动效果的方法,通过设置变量INDEX_CATEGORY_CHECKING避免递归调用,实现父子节点状态同步更新,包括当父节点被选中时所有子节点的状态变更,以及子节点状态变更对父节点的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自:  http://www.cnblogs.com/KingStar/archive/2010/06/10/1755299.html

 

经典的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;
    }
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值