Peter Shawky
Peter Shawky

Reputation: 1

How to make parent node in treeview checked if only one from its child nodes checked , and unchecked if all child nodes unchecked too

How to make parent node in treeview checked if only one from its child nodes checked , and unchecked if all child nodes unchecked too.

i am using this script

 <script type="text/javascript">
     $(function () {

         $(".MyTreeView").find(":checkbox").change(function () {
             //check or uncheck childs
             var nextele = $(this).closest("table").next()[0];
             if (nextele&&nextele.tagName == "DIV")
             { 
                 $(nextele).find(":checkbox").prop("checked", $(this).prop("checked"));

             }
             //check nodes all with the recursive method
             CheckChildNodes($(".MyTreeView").find(":checkbox").first()); 

         });
         //method check filial nodes
         function CheckChildNodes(Parentnode)
         {

             var nextele = $(Parentnode).closest("table").next()[0];

             if (nextele && nextele.tagName == "DIV") {
                 $(nextele).find(":checkbox").each(function () {
                     CheckChildNodes($(this));
                 });

                 if ($(nextele).find("input:checked").length == 0) {
                     $(Parentnode).removeAttr("checked");
                 }
                 if ($(nextele).find("input:checked").length > 0) {
                     $(Parentnode).prop("checked", "checked");
                 }

             }
             else { return; } 

         }

     }) 
     </script>

but it works only for the first node , i want to apply it for all other nodes. this is link to my testing page http://72.41.52.131/erp/Default4.aspx

Upvotes: 0

Views: 4508

Answers (1)

Peter Shawky
Peter Shawky

Reputation: 1

I solved my problem i used this answer and it works fine [child nodes in treeview control automatically checked when parent node checked

Thanks

Upvotes: 0

Related Questions