javascript 满足多层treeview的各种勾选
这段时间在微软团队里给上海贝尔做EHR项目,总的来说上海贝尔的食堂还会不错的,可能是目前为止吃过最好吃的食堂了吧。
这个项目有一点我有点不喜欢,他们在项目里用到了第三方的Telerik,我觉得比较慢,于是基本还是自己写。下面这个就是treeview的各种勾选要求,本银完全用js完成。
var childIds = ""; function GetChildIdArray2(parentNode) { if (parentNode == null) { return; } var childNodes = parentNode.children; var count = childNodes.length; for (var i = 0; i < count; i++) { var tmpNode = childNodes[i]; if (tmpNode.tagName == "INPUT" && tmpNode.type == "checkbox") { childIds = tmpNode.id + ":" + childIds; } GetChildIdArray2(tmpNode); } } function SetChildCheckBox(parentNode, checked) { if (parentNode == null) { return; } var childNodes = parentNode.children; var count = childNodes.length; for (var i = 0; i < count; i++) { var tmpNode = childNodes[i]; if (tmpNode.tagName == "INPUT" && tmpNode.type == "checkbox") { tmpNode.checked = checked; } SetChildCheckBox(tmpNode, checked); } } function SetParentCheckBox(childNode,tag) { if (childNode == null) { return; } var parent = childNode.parentNode; if (parent == null || parent == "undefined") { return; } do { parent = parent.parentNode; } while (parent && parent.tagName != "DIV"); if (parent == "undefined" || parent == null) { return; } var parentId = parent.getAttribute("ID"); if (parentId == "") return; var objParent = document.getElementById(parentId); childIds = ""; GetChildIdArray2(objParent); childIds = childIds.substring(0, childIds.length - 1); var aryChild = childIds.split(":"); var result = false; for (var i in aryChild) { var childCk = document.getElementById(aryChild[i]); if (childCk.checked == true) { result = true; break; } } var result2 = true; for (var i in aryChild) { var childCk = document.getElementById(aryChild[i]); if (childCk.checked == false) { result2 = false; break; } } parentId = parentId.replace("Nodes", "CheckBox"); var parentCk = document.getElementById(parentId); if (parentCk == null) { return; } if (tag == 0) { if (result) { parentCk.checked = true; } else { parentCk.checked = false; } } else if (tag == 1) { if (result2) { parentCk.checked = true; } else { parentCk.checked = false; } } else { } SetParentCheckBox(parentCk,tag); } //有子勾选父就勾选(递归) function OnCheckEvent() { var objNode = event.srcElement; if (objNode.tagName != "INPUT" || objNode.type != "checkbox") { return; } var ck_ID = objNode.getAttribute("ID"); var node_ID = ck_ID.substring(0, ck_ID.indexOf("CheckBox")) + "Nodes"; var curTreeNode = document.getElementById(node_ID); SetChildCheckBox(curTreeNode, objNode.checked); SetParentCheckBox(objNode,0); } //子全部勾选父才勾选(递归) function OnCheckEvent_P() { var objNode = event.srcElement; if (objNode.tagName != "INPUT" || objNode.type != "checkbox") { return; } var ck_ID = objNode.getAttribute("ID"); var node_ID = ck_ID.substring(0, ck_ID.indexOf("CheckBox")) + "Nodes"; var curTreeNode = document.getElementById(node_ID); SetChildCheckBox(curTreeNode, objNode.checked); SetParentCheckBox(objNode,1); }