Asp.Net TreeView with CheckBox

12374 단어 checkbox
1 상위 노드를 선택하면 모든 하위 노드가 선택됩니다.2 하위 노드가 하나만 선택되면 상위 노드가 선택됩니다.
<script language="javascript" type="text/javascript">

function HandleCheckbox()                                           

{                                                                              

    var element = event.srcElement;                                            

    if (element.tagName == "INPUT" && element.type == "checkbox")              

    {                                                                          

        var checkedState = element.checked;                                   

        while (element.tagName != "TABLE") // Get wrapping table               

        {                                                                      

            element = element.parentElement;                                   

        }                                                                            

        var parentElement = element;

                   

        if(checkedState)

        {

         CheckParents(element);

        } 

                                                                                     

        element = element.nextSibling; //element.tagName = DIV 

                                                                              

        if (element != null) // If no childrens then exit 

        {                                                                                                     

            var childTables = element.getElementsByTagName("TABLE"); 

            

            for (var tableIndex = 0; tableIndex < childTables.length; tableIndex++)

            {                                                                      

                CheckTable(childTables[tableIndex], checkedState);      

            } 

        }

        if(checkedState == false)

        {

            UnCheckParents(parentElement);

        }            

                                                            

    }                                                                          

}                                                                              

                                                                               

// Uncheck the parents of the given table, Can remove the recurse (redundant) 

function CheckParents(table)                                      

{                                                                              

    if (table == null || table.rows[0].cells.length == 2) // This is the root 

    {                                                                          

        return;                                                                

    }                                                                          

    var parentTable = table.parentElement.previousSibling;

    CheckTable(parentTable, true);                                 

    CheckParents(parentTable);                                    

}  



// Check the parents of the given table, Can remove the recurse (redundant) 

function UnCheckParents(table)                                      

{       

                                                                     

    if (table == null || table.rows[0].cells.length == 2) // This is the root  

    {                                                                          

        return;                                                                

    }                                                                    

    var parentTable = table.parentElement.previousSibling; 

   

    var checkedCount  =GetCheckedCount(table.parentElement);

    if(checkedCount == 0)

    {

        CheckTable(parentTable, false);

    }                                                 

    UnCheckParents(parentTable);                                    

}                                                                              

                                                                               

// Handle the set of checkbox checked state                                    

function CheckTable(table, checked) 

    {                                                                              

    var checkboxIndex = table.rows[0].cells.length - 1;   

    var cell = table.rows[0].cells[checkboxIndex];  

    var checkboxes = cell.getElementsByTagName("INPUT"); 

    if (checkboxes.length == 1) 

    {                                                                          

        checkboxes[0].checked = checked; 

    } 

                                                                           

}  

//Get checked children count

function GetCheckedCount(table)

{

    var checkedCount = 0; 

    var element = table.nextSibling;

    var childTable = table.getElementsByTagName("TABLE");

    

    for(var tableIndex = 0;tableIndex < childTable.length; tableIndex++)

    {

        var childTables = childTable[tableIndex];

        var checkboxIndex = childTables.rows[0].cells.length - 1;    

        var cell = childTables.rows[0].cells[checkboxIndex];  

        var checkboxes = cell.getElementsByTagName("INPUT");

        if (checkboxes.length == 1 && checkboxes[0].checked == true)

        {                                                                          

             checkedCount++;            

        }      

    }   

    return  checkedCount;   

}                                                                            

 </script>



<asp:TreeView ID="TreeViewModules" runat="server" ShowCheckBoxes="All" onclick="HandleCheckbox();"

        ShowLines="True" ExpandDepth="1" meta:resourcekey="TreeViewModulesResource1"></asp:TreeView>

좋은 웹페이지 즐겨찾기