TreeView里关于checkbox全选,反选的js代码

  //event.srcElement 不兼容火狐
        function client_OnTreeNodeChecked() {
            var obj = window.event.srcElement;
            var treeNodeFound = false;
            var checkedState;
            if (obj.tagName == "INPUT" && obj.type == "checkbox") {
                var treeNode = obj;
                checkedState = treeNode.checked;
                do {
                    obj = obj.parentElement;
                }
                while (obj.tagName != "TABLE")
                var parentTreeLevel = obj.rows[0].cells.length;
                var parentTreeNode = obj.rows[0].cells[0];
                var tables = obj.parentElement.getElementsByTagName("TABLE");
                var numTables = tables.length
                if (numTables >= 1) {
                    for (i = 0; i < numTables; i++) {
                        if (tables[i] == obj) {
                            treeNodeFound = true;
                            i++;
                            if (i == numTables) {
                                return;
                            }
                        }
                        if (treeNodeFound == true) {
                            var childTreeLevel = tables[i].rows[0].cells.length;
                            if (childTreeLevel > parentTreeLevel) {
                                var cell = tables[i].rows[0].cells[childTreeLevel - 1];
                                var inputs = cell.getElementsByTagName("INPUT");
                                inputs[0].checked = checkedState;
                            }
                            else {
                                return;
                            }
                        }
                    }
                }
            }
        }
        //选择或者取消祖先结点的选择
        function client_OnTreeNodeChecked1() {
            var obj = window.event.srcElement;
            var treeNodeFound = false;
            var checkedState;
            if (obj.tagName == "INPUT" && obj.type == "checkbox") {
                var treeNode = obj;
                checkedState = treeNode.checked;
                do {
                    obj = obj.parentElement;
                }
                while (obj.tagName != "TABLE")
                var objT = obj;
                do {
                    obj = obj.parentElement;
                }
                while (obj.tagName != "DIV")
                checkParNodes(obj, objT, checkedState);
            }
        }
        function checkParNodes(obj, objT, checkedState) {
            if (obj == null || obj == undefined) {
                return;
            }
            var unChecked = true;
            var treeNodeFound = false;
            if (checkedState == false) {
                var baseTreeLevel = objT.rows[0].cells.length;
                var tables = objT.parentElement.getElementsByTagName("TABLE");
                var numTables = tables.length;
                if (numTables >= 1) {
                    for (i = 0; i < numTables; i++) {
                        var childTreeLevel = tables[i].rows[0].cells.length;
                        if (baseTreeLevel != childTreeLevel) {
                            continue;
                        }
                        var cell = tables[i].rows[0].cells[childTreeLevel - 1];
                        var inputs = cell.getElementsByTagName("INPUT");
                        if (inputs[0].checked) {
                            unChecked = false;
                            break;
                        }
                        if (i == numTables) {
                            break;
                        }
                    }
                }
            }
            if (unChecked == true) {
                var id = obj.id.replace("Nodes", "");
                var pObj = document.getElementById(id + "CheckBox");
                if (pObj == null) {
                    return;
                }
                pObj.checked = checkedState;

                if (pObj.tagName == "INPUT" && pObj.type == "checkbox") {
                    do {
                        pObj = pObj.parentElement;
                    }
                    while (pObj.tagName != "TABLE")
                    var pObjT = pObj;
                    do {
                        pObj = pObj.parentElement;
                    }
                    while (pObj.tagName != "DIV")
                    checkParNodes(pObj, pObjT, checkedState);
                }

            }

 

设置treeview的onclick事件,onclick里面设置对client_OnTreeNodeChecked(),client_OnTreeNodeChecked1()函数的调用就OK了。

你可能感兴趣的:(checkbox)