Asp.net 2.0的TreeView客户端个性化控制(2)
2. 只打开一个节点(关闭其他兄弟节点)
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>LeftMenu_Tree</title>
</head>
<body bgcolor="#DDEDFD">
<form id="form1" runat="server">
<a href="javascript:TreeviewExpandCollapseAll('<%=TreeView1.ClientID%>', true)">Expand
All</a> , <a href="javascript:TreeviewExpandCollapseAll('<%=TreeView1.ClientID%>', false)">
Collapse All</a>
<asp:TreeView ID="TreeView1" SkinID="tvClass" runat="server" ShowLines="true" NodeWrap="false">
</asp:TreeView>
</form>
</body>
</html>
<script language="javascript">
//2. 只打开一个节点(关闭其他兄弟节点)------------------------------------------
function TreeView_ToggleNode(data, index, node, lineType, children)
{
var img = node.childNodes[0];
var newExpandState;
try {
//***折叠兄弟节点------------------------
CollapseBrothers(data,children);
//---------------------------------------
if (children.style.display == "none")
{
children.style.display = "block";
newExpandState = "e";
if ((typeof(img) != "undefined") && (img != null))
{
if (lineType == "l")
{
img.src = data.images[15];
}
else if (lineType == "t")
{
img.src = data.images[12];
}
else if (lineType == "-")
{
img.src = data.images[18];
}
else
{
img.src = data.images[5];
}
img.alt = data.collapseToolTip.replace(/\{0\}/, TreeView_GetNodeText(node));
}
}
else
{
children.style.display = "none";
newExpandState = "c";
if ((typeof(img) != "undefined") && (img != null))
{
if (lineType == "l")
{
img.src = data.images[14];
}
else if (lineType == "t")
{
img.src = data.images[11];
}
else if (lineType == "-")
{
img.src = data.images[17];
}
else
{
img.src = data.images[4];
}
img.alt = data.expandToolTip.replace(/\{0\}/, TreeView_GetNodeText(node));
}
}
}
catch(e) {}
data.expandState.value = data.expandState.value.substring(0, index) + newExpandState + data.expandState.value.slice(index + 1);
}
//折叠兄弟节点
function CollapseBrothers(data,childContainer)
{
var parent = childContainer.parentNode;
for(i=0; i< parent.childNodes.length; i++)
{
if(parent.childNodes[i].tagName.toLowerCase() =="div")
{
if(parent.childNodes[i].id != childContainer.id)
{
parent.childNodes[i].style.display = "none"
}
}
else if(parent.childNodes[i].tagName.toLowerCase() =="table")
{
var treeLinks = parent.childNodes[i].getElementsByTagName("a");
if(treeLinks.length > 2)
{
var j=0;
if(treeLinks[j].firstChild.tagName)
{
if(treeLinks[j].firstChild.tagName.toLowerCase() == "img")
{
var img = treeLinks[j].firstChild;
if(i==0)
img.src = data.images[8];
else if(i==parent.childNodes.length-2)
img.src = data.images[14];
else
img.src = data.images[11];
}
}
}
}
}
}
//-----------------------------------------------------------------------------
</script>