码迷,mamicode.com
首页 > 其他好文 > 详细

TreeView树Checkbox选中问题

时间:2015-09-12 12:14:57      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

//向上选中所有父节点
function setParentState(objNode) {
var objParentDiv = WebForm_GetParentByTagName(objNode, "div");
if (objParentDiv == null || objParentDiv == "undefined")
return;
var divID = objParentDiv.getAttribute("ID");
var prefix = divID.substring(0, divID.indexOf("Nodes"));
var parentID = prefix + "CheckBox";
var parentChk = document.getElementById(parentID);
if (parentChk == null || parentChk == "undefined")
return;
if (objNode.checked) {
parentChk.checked = true;
}
else {
if (isAllChildrenUnChecked(parentChk)) {
parentChk.checked = false;
}
}
setParentState(parentChk);

}

function setChildState(objNode)//设定子控件状态
{
var nodeID = objNode.getAttribute("ID"); //chkBox ID
var prefix = nodeID.substring(0, nodeID.indexOf("CheckBox")); //节点的前缀
var childrenDiv = document.getElementById(prefix + "Nodes");
if (childrenDiv == null || childrenDiv == "undefined")
return;
var childrenArray = childrenDiv.children; //取得所有子控件
for (var i = 0; i < childrenArray.length; i++) {
var container = childrenArray[i]; //子控件的容器
var chk = WebForm_GetElementByTagName(container, "input"); //查找Check控件,由于只有一个种Input控件,就是CheckBox
chk.checked = objNode.checked;
setChildState(chk);
}
}

function isAllChildrenUnChecked(objChk) {
var objChkID = objChk.getAttribute("ID");
var prefix = objChkID.substring(0, objChkID.indexOf("CheckBox")); //节点的前缀
var childrenDiv = document.getElementById(prefix + "Nodes");
if (childrenDiv == null || childrenDiv == "undefined")
return;
var childrenArray = childrenDiv.children; //取得所有子控件
for (var i = 0; i < childrenArray.length; i++) {
var container = childrenArray[i]; //子控件的容器
var chk = WebForm_GetElementByTagName(container, "input"); //查找Check控件,由于只有一个种Input控件,就是CheckBox
if (chk.checked)
return false;
}
return true;
}

function HandleCheckEvent() {
var objNode = event.srcElement;
if (objNode.tagName != "INPUT" || objNode.type != "checkbox")
return;
//设定子Chk状态
setChildState(objNode);

//设定父Chk状态
setParentState(objNode);
}

 //全选

function funSelTYAll(obj) {
var elements = document.forms[‘form1‘].elements;
for (var i = 0; i < elements.length; i++) {
if (elements[i].type == ‘checkbox‘) {
elements[i].checked = obj.checked;
}
}
}

 

 

//然后在

<asp:CheckBox ID="SetAll" runat="server" Text="全选" onclick="funSelTYAll(this);" />
<asp:TreeView ID="TreeView1" runat="server" onclick="HandleCheckEvent()" PopulateNodesFromClient="False"
ShowCheckBoxes="All" ShowLines="True" ExpandDepth="3">
</asp:TreeView>

 

TreeView树Checkbox选中问题

标签:

原文地址:http://www.cnblogs.com/vakeynb/p/4802726.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!