JS enables full selection of Treeview

Source: Internet
Author: User

JS enables full selection of Treeview

// Page Treeview controls such as: <asp: Treeview id = "tvselectuser" runat = "server" showcheckboxes = "all"> </ASP: Treeview>

// Method 1: The simplest option is "Select All". If you cancel the option, no recursion is performed and no subnode is selected.

function OnTreeNodeChecked1()    {        var ele = event.srcElement;        if (ele.type == 'checkbox')        {            var childrenDivID = ele.id.replace('CheckBox', 'Nodes');            var div = document.getElementById(childrenDivID);            if (div != null)            {                var checkBoxs = div.getElementsByTagName('INPUT');                for (var i = 0; i < checkBoxs.length; i++)                {                    if (checkBoxs[i].type == 'checkbox')                        checkBoxs[i].checked = ele.checked;                }            }        }    } 

// Call the method to add tvselectuser. Attributes. Add ("onclick", "ontreenodechecked ()") in the page_load event; or onclick = "ontreenodechecked1 ();"

// It can also be used for example: ASP: Treeview id = "tvselectuser" runat = "server" showcheckboxes = "all"
Onclick = ontreenodechecked1 () "> </ASP: Treeview>

 

 

// Method 2:
// JS implements the selection of the parent node in the Treeview, and the child node is also selected. If all the child nodes are selected, the parent node is automatically selected.
// Only Treeview is supported
// Call method Treeview. Attributes. Add ("onclick", "ontreenodechecked ()"); or onclick = "ontreenodechecked ();"

Function ontreenodechecked () {var ele = event. srcelement; If (Ele. type = 'checkbox') {var childrendivid = ELE. id. replace ('checkbox', 'nodes '); var DIV = document. getelementbyid (childrendivid); If (Div! = NULL) {var checkboxs = div. getelementsbytagname ('input'); For (VAR I = 0; I <checkboxs. length; I ++) {If (checkboxs [I]. type = 'checkbox') checkboxs [I]. checked = ELE. checked ;}} ontreenodechildchecked (Ele) ;}} function ontreenodechildchecked (Ele) {// recursive processing if (Ele = NULL) {ele = event. srcelement;} var parentdiv = ELE. parentelement ;//. parentelement. parentelement. parentelement. parentelement; var parentchk Box = Document. getelementbyid (parentdiv. Id. Replace ('nodes ', 'checkbox'); If (parentchkbox! = NULL) {var childschkall = true; var boxs = parentdiv. getelementsbytagname ('input'); For (VAR I = 0; I <boxs. length; I ++) {If (boxs [I]. type = 'checkbox' & boxs [I]. checked = false) {childschkall = false ;}} parentchkbox. checked = childschkall; ontreenodechildchecked (parentchkbox );}}

// Call the method to add tvselectuser. Attributes. Add ("onclick", "ontreenodechecked ()") in the page_load event; or onclick = "ontreenodechecked ();"

// It can also be used for example: ASP: Treeview id = "tvselectuser" runat = "server" showcheckboxes = "all"
Onclick = ontreenodechecked () "> </ASP: Treeview>

 

 

Method 3: select all. If the child node is not selected, the root node is not selected.

Function selecttreeviewnodes () {var o = event. srcelement; If (O. tagname = "input" & O. type = "checkbox") // click the Treeview checkbox to trigger {var d = O. ID; // obtain the ID of the current checkbox; var E = D. replace ("checkbox", "nodes"); // view the script information to obtain the ID var DIV = window.doc ument that contains all subnode Divs. getelementbyid (E); // obtain the DIV object if (Div! = NULL) // if it is not null, it indicates that the self-node {var check = div exists. getelementsbytagname ("input"); // obtain all the tokens starting with input in the DIV for (I = 0; I <check. length; I ++) {If (check [I]. type = "checkbox") // if it is checkbox {check [I]. checked = O. checked; // when the node status is the same as that of the parent node, that is, when all nodes are selected, the status of the parent node changes, {var divid = O. parentelement. parentelement. parentelement. parentelement. parentelement; // Div var id = divid of the subnode. id. replace ("nodes", "checkbox"); // obtain the root node ID var checkbox = divid. getelementsbytagname ("input"); // obtain the number of all subnodes var S = 0; for (I = 0; I <checkbox. length; I ++) {If (checkbox [I]. checked) // determine how many subnodes are selected {s ++;} If (S = checkbox. length) // if you select all or select the child nodes of another root node, {// The initial root node status is still selected. getelementbyid (ID ). checked = true;} else {// otherwise, the parameter Doc ument is not selected. getelementbyid (ID ). checked = false ;}}}}

Address: http://www.cnblogs.com/vihone/archive/2010/06/26/1765719.html

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.