日期:2014-05-16  浏览次数:20467 次

ExtJS 树形结构的级联选择
tree.on('checkchange', function(node, checked) {
        selParent(node, checked);
        selChild(node, checked);
    }, tree);
    //这个方法是选择父节点,自动选中所有的子节点
    function selParent(node, checked) {
        checked ? node.expand() : node.collapse();
        if (node.hasChildNodes()) {
            node.eachChild(function(child) {
                child.attributes.checked = checked;
                var cb = child.ui.checkbox;
                if (cb) cb.checked = checked;
                selParent(child, checked);
            });
        }
    }

//这个方法是选择子节点,自动选中父节点的父节点
    function selChild(node, checked) {
        if (checked) {
            node.expand();
            var parentNode = node.parentNode;
            if (parentNode != undefined) {
                parentNode.attributes.checked = checked;
                var cb = parentNode.ui.checkbox;
                if (cb) cb.checked = checked;
                selChild(parentNode, checked);
            }
        }
    }