<Script language = "JavaScript">
/*
Function Name: checknode (currentnode), parentnode (currentnode), childnode (currentnode)
Author: Chang Dongfang
Created on:
Function: implements the Treeview with checkbox.
1. Select all child nodes of the parent node.
2. After the selection of all child nodes is canceled, the selection of the parent node is also canceled.
Usage: 1. Add checknode (currentnode), parentnode (currentnode), and childnode (currentnode) in the middle of
2. Add yourtreeview. Attribute. Add ("oncheck", "checknode (yourtreeview. gettreenode (yourtreeview. clickednodeindex)") to the *. aspx. CS page_load () event ))")
*/
// Recursively traverse all subnodes
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;
}
// Process the parent node
If (currentnode. getparent ()! = NULL)
{
// Select and Process
If (currentnode. getattribute ('checked '))
{
Parentnode (currentnode );
}
Else
// Deselect
{
Childnode (currentnode );
}
}
Else
{
// Do nothing
}
// Subnode 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 parent node of the previous Layer
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 parent node of the previous Layer
Childnode (currentnode. getparent ());
}
}
</SCRIPT>
How does JS recursively traverse all subnodes of a node in the Treeview
VaR allrootnode = new array ();
Allrootnode = treeview1.getchildren ();
Alertnode (allrootnode );
Function alertnode (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 ());
}
}
}