JS control asp.net the TreeView control Select All or Cancel (sample code) _javascript Tips

Source: Internet
Author: User

Copy Code code as follows:

<script language= "JavaScript" >
/*
Function name: Checknode (CurrentNode), ParentNode (CurrentNode), Childnode (CurrentNode)

function function: To implement a TreeView with a checkbox
1, select the parent node of its child nodes are also selected.
2, the elimination of all the sub-node selection, the choice of the parent node will also be canceled

How to use:
1. Add Checknode (CurrentNode), ParentNode (CurrentNode), Childnode (CurrentNode) in the middle of

2. Add YourTreeView.Attribute.Add to the Page_Load () event in *.aspx.cs ("Oncheck", "Checknode ( Yourtreeview.clickednodeindex)) ")
*/

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)
{
Check processing
if (Currentnode.getattribute (' Checked '))
{
ParentNode (CurrentNode);
}
Else
Uncheck
{
Childnode (CurrentNode);
}
}
Else
{
Don't do anything.
}
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 parent node
function ParentNode (CurrentNode)
{
if (Currentnode.getparent ()!=null)
{
Currentnode.getparent (). setattribute (' Checked ', true);
Recursively calls ParentNode (CurrentNode) to traverse a parent node on a higher level
ParentNode (Currentnode.getparent ());
}
}
Recursively uncheck 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 calls Childnode (CurrentNode) to traverse a parent node on a higher level
Childnode (Currentnode.getparent ());
}
}

</script>


The problem of JS recursively traversing all child nodes of a node in the TreeView
Copy Code code as follows:

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 ());
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.