`

TreePanel checkbox 联动

阅读更多

转载自:  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;
    }
}
 
分享到:
评论
4 楼 明天的昨天 2010-12-10  
dengli19881102 写道
还有INDEX_CATEGORY_CHECKING 这个事在哪定义的?

对于未定义的变量 if的条件"!未定义变量" 将返回真。 这里把这个特性运用了下
3 楼 明天的昨天 2010-12-10  
dengli19881102 写道
if (node.parentNode && node.parentNode != t.root) {

这个t.root指的是什么啊,
t这个对象没有被定义啊

checkchange 是树的一个事件。 这里只是部分代码。 首先你肯定要定义tree
2 楼 dengli19881102 2010-12-10  
还有INDEX_CATEGORY_CHECKING 这个事在哪定义的?
1 楼 dengli19881102 2010-12-10  
if (node.parentNode && node.parentNode != t.root) {

这个t.root指的是什么啊,
t这个对象没有被定义啊

相关推荐

Global site tag (gtag.js) - Google Analytics