Problems with get_checked()

2 posts, 0 answers
  1. Huw Lloyd
    Huw Lloyd avatar
    20 posts
    Member since:
    Jul 2009

    Posted 18 Aug 2009 Link to this post

    Hi

    I have a tree view with check boxes and I want to use client side script to

    1. If I un-check a box then I want to un-check all children
    2. If I check a box then I want to check all the parent items all the way up the hierarchy of the tree

    The code I have to do this is

      
     function updateAllChildren(nodes) 
                        { 
                           var i; 
                           for (i=0; i<nodes.get_count(); i++) 
                           { 
                               nodes.getNode(i).set_checked(false); 
                                
                               if (nodes.getNode(i).get_nodes().get_count()> 0) 
                               { 
                                   updateAllChildren(nodes.getNode(i).get_nodes()); 
                               } 
                           } 
                        } 
                         
                        function clientNodeChecked(sender, eventArgs) 
                        { 
                            var selectedNode = eventArgs.get_node(); 
                            var isSelected = selectedNode.get_checked(); 
                            var currentNode = selectedNode.get_parent(); 
                            var childNodes = selectedNode.get_nodes(); 
                         
                            // toggle node 
                            selectedNode.set_checked(!isSelected); 
                         
                            if (isSelected) // If seleceted then push changes up hierarchy 
                            { 
                                while (currentNode.get_attributes().getAttribute("ParentID") != "0") 
                                { 
                                    currentNode.set_checked(true); 
                                    currentNodecurrentNode = currentNode.get_parent(); 
                                } 
                            } 
                             
                            if (!isSelected)// force uncheck for all children 
                            { 
                                updateAllChildren(childNodes); 
                            } 
                             
                         
                        
                    } 

    The code appears to be working to a degree in that

    1. If I check a child item then it correctly sets all the parent items up the tree to checked
    2. If I un-check an item it correctly un-checks all the children entries

    The problem I am having is that if I

    1. When I try to check an item that has children then nothing happens - The variable isSelected is set to false, when it should be true.
    2. When I try to un-select an item that has no children then nothing happens - The variable isSelected is set to true when it should be false.

    I can get it working fine with server side code, but client side would be much neater

    Many thanks in advance

  2. Huw Lloyd
    Huw Lloyd avatar
    20 posts
    Member since:
    Jul 2009

    Posted 18 Aug 2009 Link to this post

    No problem I see my error - I was stupidly changing the checked status in the code with

    // toggle node 
                            selectedNode.set_checked(!isSelected); 

    When of course the checked status was already changed before the function was called.
Back to Top