ASP.NET 2.0 Treeview Checkboxes - Check All - Javascript

Rate It (6)

Last post 04-11-2008 5:21 AM by mandar44. 86 replies.

Sort Posts:

  • Re: ASP.NET 2.0 Treeview Checkboxes - Check/Uncheck child and parent - Javascript

    02-05-2008, 11:29 PM
    • Loading...
    • den2005
    • Joined on 03-02-2006, 2:08 AM
    • Philippines
    • Posts 291

    Hi pushp_aspnet,

       Not quite good at javascript..Can you modify your script "function OnTreeClick(evt)"? What I needed is when I check a node, all nodes (parent) above are checked and no child nodes below are checked including siblings on same level, and when I unchecked a node, no all parent node above is unchecked, only all child nodes below are unchecked...

    Sample Data: (Item+<number> is for references only) 

    Item1 Parent Node 1

    Item2    Child Node 1

    Item3       Sub-Child Node 1

    Item4           Sub-Sub Child Node 1

    Item5           Sub-Sub Child Node 2

    Item6       Sub-Child Node 2 

    Item7 Parent Node 2

    Item8    Child Node 1

    Item9      Sub-Child Node 1

     

    Case 1: Checking a node

       If I checked Item4 "Sub-Sub Child Node 1", Item 5,7,8,9 are Not checked, but from Item1 to Item4 and Item 6 are all checked.

    Case 2: Unchecking a Node

      If I unchecked Item3, in the previously checked Items 1,2,3,4 and 6, only Item 3 and 4 are unchecked, the rest remain checked

    Thanks in advanced.

  • Re: ASP.NET 2.0 Treeview Checkboxes - Check/Uncheck child and parent - Javascript

    02-11-2008, 6:52 PM
    • Loading...
    • deddy_one
    • Joined on 11-01-2007, 10:03 AM
    • Posts 65

    this thread rocks.

    I thank anyone here that posted the js code.

    it's a god sent for treeview control.

  • Re: ASP.NET 2.0 Treeview Checkboxes - Check/Uncheck child and parent - Javascript

    02-11-2008, 8:40 PM
    • Loading...
    • den2005
    • Joined on 03-02-2006, 2:08 AM
    • Philippines
    • Posts 291

    I have solved the problem.Big Smile

     

    function OnTreeClick(evt)
       {
            var src = window.event != window.undefined ? window.event.srcElement : evt.target;
            var obj = window.event.srcElement; 
            var isChkBoxClick = (src.tagName.toLowerCase() == "input" && src.type == "checkbox");
            if(isChkBoxClick)
            {
                var parentTable = GetParentByTagName("table", src);
                var nxtSibling = parentTable.nextSibling;
                
                do 
    			{
    				obj = obj.parentElement; 
    			} 
    			while (obj.tagName != "TABLE") 
    			
    			var tables = obj.parentElement.getElementsByTagName("TABLE");
    			
    			//get current node level 
    			var nodeTreeLevel = 0;
    			if (tables[0] == obj)
    			{
    				nodeTreeLevel = tables[0].rows[0].cells.length;
    			}			
    
                if(nxtSibling)
                {
    				var parentDiv = GetParentByTagName("div", src);      
    				//check or uncheck children at all levels
                    CheckUncheckChildren(nodeTreeLevel,parentTable.nextSibling, src.checked);
                }
                //check or uncheck parents at all levels
                CheckUncheckParents(src, src.checked);
            }
       } 
       function CheckUncheckChildren(childContainer, check)
       {
          var childChkBoxes = childContainer.getElementsByTagName("input");
          var childChkBoxCount = childChkBoxes.length;
          
          for(var i = 0; iif (check == false){
    			 alert("the checkbox is " + check + " for " + childChkBoxes[i].title);
    			 childChkBoxes[i].checked = check;
    		  }
          }
       }
       function CheckUncheckChildren(nodeLevel,childContainer, check)
       { 
    		var childChkBoxes = childContainer.getElementsByTagName("input");
    		var childTreeLevel = 0;
    		if (childContainer.rows != null)
    			childTreeLevel = childContainer.rows[0].cells.length;
    
    		var childChkBoxCount = childChkBoxes.length;
    
    		for(var i = 0; iif (check == false){
    				if (nodeLevel != childTreeLevel)
    					childChkBoxes[i].checked = check;
    			}
    		}
       }
       function CheckUncheckParents(srcChild, check)
       {
           var parentDiv = GetParentByTagName("div", srcChild);
           var parentNodeTable = parentDiv.previousSibling;
         		       
           if(parentNodeTable)
            {           
                var inpElemsInParentTable = parentNodeTable.getElementsByTagName("input");
                if(inpElemsInParentTable.length > 0)
                {
                    var parentNodeChkBox = inpElemsInParentTable[0]; 
                    if (check)
                    {
    					parentNodeChkBox.checked = check;
    					//do the same recursively
    					CheckUncheckParents(parentNodeChkBox, check);
    				}
                }
            }
       }
       function AreAllSiblingsChecked(chkBox)
       {
         var parentDiv = GetParentByTagName("div", chkBox);
         var childCount = parentDiv.childNodes.length;
         for(var i=0; iif(parentDiv.childNodes[i].nodeType == 1) //check if the child node is an element node
            {
                if(parentDiv.childNodes[i].tagName.toLowerCase() == "table")
                {
                   var prevChkBox = parentDiv.childNodes[i].getElementsByTagName("input")[0];
                  //if any of sibling nodes are not checked, return false
                  if(prevChkBox.checked) 
                  {
                    return false;
                  } 
                }
            }
         }
         return true;
       }
     
       //utility function to get the container of an element by tagname
       function GetParentByTagName(parentTagName, childElementObj)
       {
          var parent = childElementObj.parentNode;
          while(parent.tagName.toLowerCase() != parentTagName.toLowerCase())
          {
             parent = parent.parentNode;
          }
          return parent;    
       }
     
  • Re: ASP.NET 2.0 Treeview Checkboxes - Check All - Javascript

    02-26-2008, 8:48 AM
    • Loading...
    • bihagsasp.net
    • Joined on 01-25-2008, 9:40 AM
    • Ahmedabad
    • Posts 49

    Hi Avanti.....

    Just Superb Code.....! This is what I reuqired......And this is what should be logically.....in Treeview with Checkboxes...

    Thousands of thankx.....for this Superb code....!

    -Bihag Thaker

    -Bihag Thaker
    http://bihagthaker.blogspot.com/
    ----------------------------------------------
    Please 'Mark as Answer' if this post has helped you!
  • Re: ASP.NET 2.0 Treeview Checkboxes - Check/Uncheck child and parent - Javascript

    03-04-2008, 6:04 AM
    • Loading...
    • pedro.gmr
    • Joined on 01-22-2008, 2:51 PM
    • Posts 18

    Hi den2005,

     I try tu use your code in my page but it it gives me an error in the for cycles.

    den2005:
    for(var i = 0; iif (check == false){

    den2005:
    for(var i = 0; iif (check == false){

    den2005:
    for(var i=0; iif(parentDiv.childNodes[i].nodeType == 1)

    I don't understand what "iif" do.

    There is another way of doing this cycle? 

    Thanks. 

     

    Filed under:
  • Re: ASP.NET 2.0 Treeview Checkboxes - Check/Uncheck child and parent - Javascript

    03-04-2008, 6:04 AM
    • Loading...
    • pedro.gmr
    • Joined on 01-22-2008, 2:51 PM
    • Posts 18

    Hi den2005,

     I try tu use your code in my page but it it gives me an error in the for cycles.

    den2005:
    for(var i = 0; iif (check == false){

    den2005:
    for(var i = 0; iif (check == false){

    den2005:
    for(var i=0; iif(parentDiv.childNodes[i].nodeType == 1)

    I don't understand what "iif" do.

    There is another way of doing this cycle? 

    Thanks. 

     

    Filed under:
  • Re: ASP.NET 2.0 Treeview Checkboxes - Check All - Javascript

    04-01-2008, 2:54 AM
    • Loading...
    • lokanadham
    • Joined on 01-22-2008, 4:23 PM
    • Posts 79

    Hi All,

            Can someone explain me what is this TABLE used in the script?

            I am not able to understand it..

            It will be greatful if you respond.

            Thanks alot in advance.

          

     

    Lokanadham M.
    Hyderabad.
  • Re: ASP.NET 2.0 Treeview Checkboxes - Check All - Javascript

    04-01-2008, 3:07 AM
    • Loading...
    • GillouX
    • Joined on 06-03-2007, 3:28 PM
    • Belgium
    • Posts 219

    the treeview structure is made with a table

  • Re: ASP.NET 2.0 Treeview Checkboxes - Check All - Javascript

    04-01-2008, 3:25 AM
    • Loading...
    • lokanadham
    • Joined on 01-22-2008, 4:23 PM
    • Posts 79

    Thank you so much.

    Can we access the treeview with that TABLE and How do we access it?

    Can you please explain me this?

    Lokanadham M.
    Hyderabad.
  • Re: ASP.NET 2.0 Treeview Checkboxes - Check/Uncheck child and parent - Javascript

    04-01-2008, 3:34 AM
    • Loading...
    • den2005
    • Joined on 03-02-2006, 2:08 AM
    • Philippines
    • Posts 291

    Sorry about that pedro,

       I just post it without checking..Big Smile

     function OnTreeClick(evt)
       {
            var src = window.event != window.undefined ? window.event.srcElement : evt.target;
            var obj = window.event.srcElement;
            var isChkBoxClick = (src.tagName.toLowerCase() == "input" && src.type == "checkbox");
            if(isChkBoxClick)
            {
                var parentTable = GetParentByTagName("table", src);
                var nxtSibling = parentTable.nextSibling;
               
                do
       {
        obj = obj.parentElement;
       }
       while (obj.tagName != "TABLE")
       
       var tables = obj.parentElement.getElementsByTagName("TABLE");
       
       //get current node level
       var nodeTreeLevel = 0;
       if (tables[0] == obj)
       {
        nodeTreeLevel = tables[0].rows[0].cells.length;
       }   

                if(nxtSibling)
                {
        var parentDiv = GetParentByTagName("div", src);     
        //check or uncheck children at all levels
                    CheckUncheckChildren(nodeTreeLevel,parentTable.nextSibling, src.checked);
                }
                //check or uncheck parents at all levels
                CheckUncheckParents(src, src.checked);
            }
       }
       function CheckUncheckChildren(childContainer, check)
       {
          var childChkBoxes = childContainer.getElementsByTagName("input");
          var childChkBoxCount = childChkBoxes.length;
         
          for(var i = 0; i<childChkBoxCount; i++)
          {
        if (check == false){
        alert("the checkbox is " + check + " for " + childChkBoxes[i].title);
        childChkBoxes[i].checked = check;
        }
          }
       }
       function CheckUncheckChildren(nodeLevel,childContainer, check)
       {
      var childChkBoxes = childContainer.getElementsByTagName("input");
      var childTreeLevel = 0;
      if (childContainer.rows != null)
       childTreeLevel = childContainer.rows[0].cells.length;

      var childChkBoxCount = childChkBoxes.length;

      for(var i = 0; i<childChkBoxCount; i++)
      {
       if (check == false){
        if (nodeLevel != childTreeLevel)
         childChkBoxes[i].checked = check;
       }
      }
       }
       function CheckUncheckParents(srcChild, check)
       {
           var parentDiv = GetParentByTagName("div", srcChild);
           var parentNodeTable = parentDiv.previousSibling;
                 
           if(parentNodeTable)
            {          
                var inpElemsInParentTable = parentNodeTable.getElementsByTagName("input");
                if(inpElemsInParentTable.length > 0)
                {
                    var parentNodeChkBox = inpElemsInParentTable[0];
                    if (check)
                    {
         parentNodeChkBox.checked = check;
         //do the same recursively
         CheckUncheckParents(parentNodeChkBox, check);
        }
                }
            }
       }
       function AreAllSiblingsChecked(chkBox)
       {
         var parentDiv = GetParentByTagName("div", chkBox);
         var childCount = parentDiv.childNodes.length;
         for(var i=0; i<childCount; i++)
         {
            if(parentDiv.childNodes[i].nodeType == 1) //check if the child node is an element node
            {
                if(parentDiv.childNodes[i].tagName.toLowerCase() == "table")
                {
                   var prevChkBox = parentDiv.childNodes[i].getElementsByTagName("input")[0];
                  //if any of sibling nodes are not checked, return false
                  if(prevChkBox.checked)
                  {
                    return false;
                  }
                }
            }
         }
         return true;
       }
     
       //utility function to get the container of an element by tagname
       function GetParentByTagName(parentTagName, childElementObj)
       {
          var parent = childElementObj.parentNode;
          while(parent.tagName.toLowerCase() != parentTagName.toLowerCase())
          {
             parent = parent.parentNode;
          }
          return parent;   
       }

  • Re: ASP.NET 2.0 Treeview Checkboxes - Check/Uncheck child and parent - Javascript

    04-01-2008, 7:00 AM
    • Loading...
    • pedro.gmr
    • Joined on 01-22-2008, 2:51 PM
    • Posts 18

    Hi den2005,

    First, thanks for your help.

    But, as I was with some hurry, I resolve by anotherway.
    Here you can see my post: http://forums.asp.net/t/1241344.aspx
     
    Anyway, I apreciate your concern. Thanks again. 
  • Re: ASP.NET 2.0 Treeview Checkboxes - Check All - Javascript

    04-11-2008, 5:21 AM
    • Loading...
    • mandar44
    • Joined on 04-11-2008, 5:19 AM
    • Posts 1

    =============================

    The script works just fine...

    great post my friend..!!!

    I have another problem, please help me.

    On Button Click ...

    1) I want to get the text, value & deapth of the node checked. (may be single or multiple nodes)

    2) should alert if its a Root Node.

    3) It should be the Leaf node ( Last node in branch ). and not parent node...!!

    4) And -- should open a page by passing these 3 values ( in modal window )

     

    Can u help me friends ??

    Regards,

    Mandar

Page 6 of 6 (87 items) « First ... < Previous 2 3 4 5 6