none
TreeView + CheckBox RRS feed

  • Pergunta

  • Boa tarde, já tenho meu TreeView populado com seus respectivos CheckBoxes, porém, não estou conseguindo atribuir o evento : Assim que ele clicar no CheckBox eu capturar esse evento de Checked. como faço isso? pois preciso que assim que ele clicar no Nó Filho, marque como Checked no Nó Pai.

    Framework 4.0 ASP.NET


    Se a resposta foi útil por favor qualifique. Italo Biguzi Duarte, Desenvolvedor ASP.NET C#, Italo.biguzzi@gmail.com


    quinta-feira, 27 de setembro de 2012 18:24

Respostas

  • Adicione o seguinte javascript em minha página e funcionou.

    function client_OnTreeNodeChecked()
    {
    var obj = window.event.srcElement;
    var treeNodeFound = false;
    var checkedState;
    if (obj.tagName == "INPUT" && obj.type == "checkbox") {
    var treeNode = obj;
    checkedState = treeNode.checked;
    do
    {
    obj = obj.parentElement;
    } while (obj.tagName != "TABLE")
    var parentTreeLevel = obj.rows[0].cells.length;
    var parentTreeNode = obj.rows[0].cells[0];
    var tables = obj.parentElement.getElementsByTagName("TABLE");
    if(obj.tagName == "TABLE") 
    {
       if (treeNode.checked) //if any child is checked check parent
       {
         //head1 gets the parent node of the checked node 
          var head1 = obj.parentElement.previousSibling; 
          if(head1!=null)
          {
            if(head1.tagName == "TABLE") 
            {
                //checks for the input tag which consists of checkbox 
                var matchElement1 = head1.getElementsByTagName("INPUT"); 
                //matchElement1[0] gives us the checkbox and it is checked 
                if(matchElement1.length>0)
                    matchElement1[0].checked = true; 
            }
          }
          else {
                    head1 = obj.parentElement.previousSibling;
                } 
                if(head1!=null)
                {
                    if(head1.tagName == "TABLE") 
                    {
                        //head2 gets the parent node of the checked node 
                        var head2 = obj.parentElement.parentElement.previousSibling; 
                        if(head2!=null)
                        {
                            if(head2.tagName == "TABLE") 
                            {
                                //checks for the input tag which consists of checkbox 
                                var matchElement2 = head2.getElementsByTagName("INPUT"); 
                                if(matchElement2.length>0)
                                     matchElement2[0].checked = true; 
                            }
                        }
                    }
                }
                else {
                        head2 = obj.parentElement.previousSibling;
                     } 
                if(head2!=null)
                {
                    if(head2.tagName == "TABLE") 
                    {
                        //head3 gets the parent node of the checked node 
                        var head3 = obj.parentElement.parentElement.parentElement.previousSibling; 
                        if(head3!=null)
                        {
                            if(head3.tagName == "TABLE")
                            {
                                //checks for the input tag which consists of checkbox 
                                var matchElement3 = head3.getElementsByTagName("INPUT"); 
                                if(matchElement3.length>0)
                                    matchElement3[0].checked = true; 
                            }
                         }
                    }
                }
                else {
                        head3 = obj.parentElement.previousSibling;
                     } 
                if(head3!=null)
                {
                    if(head3.tagName == "TABLE") 
                    { 
                        //head4 gets the parent node of the checked node 
                        var head4 = obj.parentElement.parentElement.parentElement.parentElement.previousSibling; 
                        if(head4 != null) 
                        { 
                            if(head4.tagName == "TABLE") 
                            { 
                                //checks for the input tag which consists of checkbox 
                                var matchElement4 = head4.getElementsByTagName("INPUT"); 
                                if(matchElement4.length>0)
                                    matchElement4[0].checked = true; 
                            }
                        }
                    } 
                }
    
    }//end if - checked
    if (!treeNode.checked)//if all child are unchecked then uncheck parent 
       {
        var chk1 = false;
        var head1 = obj.parentElement.previousSibling;
        var pTreeLevel1 = obj.rows[0].cells.length;
        if(head1!=null)
        {
        if(head1.tagName == "TABLE") 
        {
            var tbls = obj.parentElement.getElementsByTagName("TABLE");
            var tblsCount = tbls.length;
            for (i=0; i < tblsCount; i++) 
            {
                var childTreeLevel = tbls[i].rows[0].cells.length;
                if (childTreeLevel = pTreeLevel1) 
                {
                    var chld = tbls[i].getElementsByTagName("INPUT");
                    if(chld.length>0)
                    {
                        if (chld[0].checked == true) 
                        {
                            chk1 = true;
                            break;
                        }
                    }
                }
            }
            var nd = head1.getElementsByTagName("INPUT");
            if(nd.length>0){
            nd[0].checked = chk1;}
        }
        }
        else
        {
            head1 = obj.parentElement.previousSibling;
        }
        var chk2 = false;
        if(head1!=null)
        {
        if(head1.tagName == "TABLE") 
        {
            var head2 = obj.parentElement.parentElement.previousSibling; 
            if(head2!=null)
            {
            if(head2.tagName == "TABLE") 
            {
                var tbls = head1.parentElement.getElementsByTagName("TABLE");
                var pTreeLevel2 = head1.rows[0].cells.length;
                var tblsCount = tbls.length;
                for (i=0; i < tblsCount; i++) 
                {
                    var childTreeLevel = tbls[i].rows[0].cells.length;
                    if (childTreeLevel = pTreeLevel2) 
                    {
                      var chld = tbls[i].getElementsByTagName("INPUT");
                      if(chld.length>0)
                      {
                          if (chld[0].checked == true) 
                          {
                            chk2 = true;
                            break;
                          }
                      }
                    }
                }
                var nd = head2.getElementsByTagName("INPUT");
                if(nd.length>0){
                nd[0].checked = (chk2 && chk1);}
            }
            }
        }
        }
        else 
        {
            head2 = obj.parentElement.previousSibling;
        }
        var chk3 = false;
        if(head2!=null)
        {
        if(head2.tagName == "TABLE") 
        {
            var head3 = obj.parentElement.parentElement.parentElement.previousSibling; 
            if(head3!=null)
            {
            if(head3.tagName == "TABLE") 
            {
        
                var tbls = head2.parentElement.getElementsByTagName("TABLE");
                var pTreeLevel3 = head2.rows[0].cells.length;
                var tblsCount = tbls.length;
                for (i=0; i < tblsCount; i++) 
                {
                    var childTreeLevel = tbls[i].rows[0].cells.length;
                    if (childTreeLevel = pTreeLevel3) 
                    {
                        var chld = tbls[i].getElementsByTagName("INPUT");
                        if(chld.length>0)
                        {
                            if (chld[0].checked == true) 
                            {
                                chk3 = true;
                                break;
                            }
                        }
                    }
                }   
                var nd = head3.getElementsByTagName("INPUT");
                if(nd.length>0){
                nd[0].checked = (chk3 && chk2 && chk1);}
            }
            }
        }
        }
        else
         {
            head3 = obj.parentElement.previousSibling;
         }
        var chk4 = false;
        if(head3!=null)
        {
            if(head3.tagName == "TABLE") 
            {
                var head4 = obj.parentElement.parentElement.parentElement.parentElement.previousSibling; 
                if(head4!=null)
                {
                    if(head4.tagName == "TABLE") 
                    {
                        var tbls = head3.parentElement.getElementsByTagName("TABLE");
                        var pTreeLevel4 = head3.rows[0].cells.length;
                        var tblsCount = tbls.length;
                        for (i=0; i < tblsCount; i++) 
                        {
                            var childTreeLevel = tbls[i].rows[0].cells.length;
                            if (childTreeLevel = pTreeLevel4) 
                            {
                                var chld = tbls[i].getElementsByTagName("INPUT");
                                if(chld.length>0)
                                {
                                    if (chld[0].checked == true) 
                                    {
                                        chk4 = true;
                                        break;
                                    }
                                }
                            }
                        }
                        var nd = head4.getElementsByTagName("INPUT");
                        if(nd.length>0){
                        nd[0].checked = (chk4 && chk3 && chk2 && chk1);}
                    }
                }
            }
        }
    }//end if - unchecked
    }
    var numTables = tables.length
    if (numTables >= 1)
    {
    for (i=0; i < numTables; i++)
    {
    if (tables[i] == obj)
    {
    treeNodeFound = true;
    i++;
    if (i == numTables)
    {
    return;
    }
    }
    if (treeNodeFound == true)
    {
    var childTreeLevel = tables[i].rows[0].cells.length;
    if (childTreeLevel > parentTreeLevel)
    {
    var cell = tables[i].rows[0].cells[childTreeLevel - 1];
    var inputs = cell.getElementsByTagName("INPUT");
    inputs[0].checked = checkedState;
    }
    else
    {
    return;
    }
    }
    }
    }
    }
    }


    Se a resposta foi útil por favor qualifique. Italo Biguzi Duarte, Desenvolvedor ASP.NET C#, Italo.biguzzi@gmail.com

    • Marcado como Resposta Harley Araujo segunda-feira, 1 de outubro de 2012 11:52
    sexta-feira, 28 de setembro de 2012 14:37