Copy the code code as follows:
<script language="javascript">
/*
Function name: CheckNode(currentNode), ParentNode(currentNode), ChildNode(currentNode)
Function: implement treeview with checkbox
1. Select the parent node and all its child nodes are also selected.
2. After canceling the selection of all child nodes, the selection of the parent node will also be cancelled.
How to use:
1. Add CheckNode(currentNode), ParentNode(currentNode), ChildNode(currentNode) in the middle of <head></head>
2. Add yourTreeView.Attribute.Add("OnCheck","CheckNode(yourTreeView.getTreeNode(yourTreeView.clickedNodeIndex))") in the Page_load() event of *.aspx.cs
*/
//Recursively traverse all child nodes
function CheckNode(currentNode)
{
var childNode=new Array();
var parentNodeChild=new Array();
var isChecked;
childNode=currentNode.getChildren();
if(currentNode.getAttribute('checked'))
{
isChecked=true;
}
else
{
isChecked=false;
}
//Parent node processing
if(currentNode.getParent()!=null)
{
//Select processing
if(currentNode.getAttribute('Checked'))
{
ParentNode(currentNode);
}
else
//Uncheck
{
ChildNode(currentNode);
}
}
else
{
//Do nothing
}
//child node processing
if(childNode.length>0)
{
for(var i=0;i<childNode.length;i++)
{
childNode.setAttribute("Checked",isChecked);
if(childNode.getChildren().length>0)
{
CheckNode(childNode);
}
}
}
}
//Recursively select the parent node
function ParentNode(currentNode)
{
if(currentNode.getParent()!=null)
{
currentNode.getParent().setAttribute('Checked',true);
//Recursively call ParentNode(currentNode) to traverse the higher-level parent node
ParentNode(currentNode.getParent());
}
}
//Recursively deselect the parent node
function ChildNode(currentNode)
{
if(currentNode.getParent()!=null)
{
var checkedCount=0;
var childNode=currentNode.getParent().getChildren();
for (var i=0;i<childNode.length;i++)
{
if(childNode.getAttribute('Checked'))
{
checkedCount++;
}
}
if(checkedCount==0)
{
currentNode.getParent().setAttribute('Checked',false);
}
//Recursively call ChildNode(currentNode) to traverse the higher-level parent node
ChildNode(currentNode.getParent());
}
}
</script>
js recursively traverses all child nodes of a node in treeview
Copy the code code as follows:
var AllRootNode=new Array();
AllRootNode=TreeView1.getChildren();
AlertNode(AllRootNode);
functionAlertNode(NodeArray)
{
if(parseInt(NodeArray.length)==0)
return;
else
{
for(i=0;i<NodeArray.length;i++)
{
var cNode;
cNode=NodeArray;
alert(cNode.getAttribute("Text"));
if(parseInt(cNode.getChildren().length)!=0)
AlertNode(cNode.getChildren());
}
}
}