Solved

de-select parents when all children in tree are de-selected

Posted on 2007-11-15
5
1,570 Views
Last Modified: 2008-02-01
Hi,
I am mainly an ASP.NET developer but have to use some JavaScript for performance reasons. It would be nice if some JavaScript guru could help me with my little problem.
I have a 3rd partyTreeView:

<radT:RadTreeView
        ID="RadTree1"
        runat="server"
        OnNodeExpand=RadTree1_NodeExpand CheckBoxes=true AfterClientCheck="AfterCheck"
        >
    </radT:RadTreeView>

When the uses (de-)selects a parent node the children are (de-)selected accordingly using this JavaScript code:
<script language=javascript>

        function UpdateAllChildren(nodes, checked)
        {

            var i;
            for (i=0; i<nodes.length; i++)
            {
                 if (checked)
                 {
                    nodes[i].Check();
                 }
                 else
                 {
                    nodes[i].UnCheck();
                 }
                 
                 if (nodes[i].Nodes.length > 0)
                 {
                    UpdateAllChildren(nodes[i].Nodes, checked);
                 }
            }
       
        }
       
        function AfterCheck(node)
        {
            UpdateAllChildren(node.Nodes, node.Checked);
        }
    </script>

Would it be possible to de-select all parent nodes when all its children are de-selected?

BTW each node has a unique value.

Many thanks in advance.

BW

Chris
0
Comment
Question by:csetzkorn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 20288953
Well, may be...   You could find out how many "checked" child nodes exist for a specific routine like:

function checkedKids( node ) {
  var result = 0;
  for ( var i = 0; i < nodes.length; i++ ) {
    result += checkedKids( node[ i ] );
  }
  return result;
}

... then, to use it, you could use something like...

if ( checkedKids( node ) == 0 ) {
  node.Uncheck();
}
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 20288961
Be careful though.  If you check the number of kids for a node which has no children, the result will be 0, and this would cause the leaf node to be unchecked.  This could propogate up the tree, if you aren't careful.
0
 
LVL 16

Expert Comment

by:gnoon
ID: 20288983
Try this

function AfterCheck(node)
{
    UpdateAllChildren(node.Nodes, node.Checked);
    if(node.Parent != null)
    {
        var anyChecked= false;
        for(var i=0; i<node.Parent.Nodes.length; i++)
        {
            if(node.Parent.Nodes[i].Checked)
            {
                anyChecked= false;
                break;
            }
        }
        node.Parent.Checked = anyChecked;
    }
}
0
 
LVL 16

Accepted Solution

by:
gnoon earned 500 total points
ID: 20289000
>node.Parent.Checked = anyChecked;

that should be

if(anyChecked) node.Parent.Check();

;-)
0
 

Author Comment

by:csetzkorn
ID: 20289492
Hi,

Thanks. Sorry but it did not work but this does:

function AfterCheck(node)
{
    UpdateAllChildren(node.Nodes, node.Checked);
   
    var hasCheckedNodes = false;
            var i;
            if(node.Parent!=null)
            {
                for(i=0;i<node.Parent.Nodes.length;i++)
                {
                    if(node.Parent.Nodes[i].Checked)
                        hasCheckedNodes = true;    
                }
                if(hasCheckedNodes == false)
                    node.Parent.UnCheck();
            }      
}
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question