Advertisement

02.02.2005 at 08:34AM PST, ID: 21298743
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.6

Expand all \ Contract All buttons on a navigation tree

Asked by AshleyBryant in JavaScript

Tags: , , ,

This one should be pretty easy for you oh so smart javascript gurus ;)  I've got a script in ASP that generates the code I have posted below from an XML file.  Everything works great except for the fact that I have no way to expand / contract all of the tree.  If someone can give me a working solution they get a lot of points.  Simple.

If you want to see original source and download the images that this script uses then go here:  http://www.aspfaqs.com/webtech/code/xmltreemenu2.zip

And here is a link to the coder's demo site:  http://www.aspfaqs.com/demos/xmltreedemo2/

<html>
<head>
     <title>PESAP v2.0 : Level Map</title>
     <link rel=stylesheet href="themes/Standard/default.css" type="text/css">
     <STYLE TYPE="text/css">
     <!--
          .node { color: black;
               font-family : "Helvetica", "Arial", "MS Sans Serif", sans-serif;
               font-size : 9pt;}
          .node A:link { color: black; text-decoration: none; }
          .node A:visited { color: black; text-decoration: none; }
          .node A:active { color: black; text-decoration: none; }
          .node A:hover { color: black; background : #CCCCCC; text-decoration: none; }
     -->
     </STYLE>
</head>
<script language=javascript>
function remote2(url){
   window.opener.location=url
   window.close()
}
</script>
<body>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
                    <tr>
                         <td>
               
                    <table border="0" cellspacing="0" cellpadding="0" width="100%">
                         <tr valign="bottom">
                             
                              <td width="31"><img class="LEVEL1" src="../images/minusonly.gif" id="1" width="31" height="16" border="0"></td>
                              <td nowrap class="node">&nbsp;Root (ID: 248)</td>
                         </tr>

                    </table>
                   
                         <table border="0" cellspacing="0" cellpadding="0" width="100%">
                              <tr valign="bottom" class="LEVEL2" id="2" style="display:">
                                   <td>
                         
                    <table border="0" cellspacing="0" cellpadding="0" width="100%">
                         <tr valign="bottom">
                              <td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td>
                              <td width="31"><img class="LEVEL3" src="../images/folderclosed.gif" id="3" width="31" height="16" border="0"></td>
                              <td nowrap class="node">&nbsp;SHELF (ID: 256)</td>
                         </tr>

                    </table>
                   
                         <table border="0" cellspacing="0" cellpadding="0" width="100%">
                              <tr valign="bottom" class="LEVEL4" id="4" style="display:none">
                                   <td>
                         
                    <table border="0" cellspacing="0" cellpadding="0" width="100%">
                         <tr valign="bottom">
                              <td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td><td width="18"><img src="../images/line.gif" width="18" height="16"></td>
                              <td width="31"><img class="LEVEL5" src="../images/folderclosed.gif" id="5" width="31" height="16" border="0"></td>
                              <td nowrap class="node">&nbsp;TOOLS (ID: 258)</td>
                         </tr>

                    </table>
                   
                         <table border="0" cellspacing="0" cellpadding="0" width="100%">
                              <tr valign="bottom" class="LEVEL6" id="6" style="display:none">
                                   <td>
                         
                    <table border="0" cellspacing="0" cellpadding="0" width="100%">
                         <tr valign="bottom">
                              <td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td><td width="18"><img src="../images/line.gif" width="18" height="16"></td><td width="18"><img src="../images/line.gif" width="18" height="16"></td>
                              <td width="31"><img src="../images/docjoin.gif" width="31" height="16" border="0"></td>
                              <td nowrap class="node">&nbsp;SAW (ID: 261)</td>
                         </tr>
                    </table>

                    <table border="0" cellspacing="0" cellpadding="0" width="100%">
                         <tr valign="bottom">
                              <td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td><td width="18"><img src="../images/line.gif" width="18" height="16"></td><td width="18"><img src="../images/line.gif" width="18" height="16"></td>
                              <td width="31"><img src="../images/docjoin.gif" width="31" height="16" border="0"></td>
                              <td nowrap class="node">&nbsp;HAMMER (ID: 262)</td>
                         </tr>
                    </table>

                    <table border="0" cellspacing="0" cellpadding="0" width="100%">
                         <tr valign="bottom">
                              <td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td><td width="18"><img src="../images/line.gif" width="18" height="16"></td><td width="18"><img src="../images/line.gif" width="18" height="16"></td>
                              <td width="31"><img src="../images/doc.gif" width="31" height="16" border="0"></td>
                              <td nowrap class="node">&nbsp;TAPE MEASURE (ID: 265)</td>
                         </tr>
                    </table>
     
                                   </td>
                              </tr>
                         </table>
                         
                    <table border="0" cellspacing="0" cellpadding="0" width="100%">
                         <tr valign="bottom">
                              <td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td><td width="18"><img src="../images/line.gif" width="18" height="16"></td>
                              <td width="31"><img class="LEVEL7" src="../images/folderclosed.gif" id="7" width="31" height="16" border="0"></td>
                              <td nowrap class="node">&nbsp;MATERIALS (ID: 259)</td>
                         </tr>

                    </table>
                   
                         <table border="0" cellspacing="0" cellpadding="0" width="100%">
                              <tr valign="bottom" class="LEVEL8" id="8" style="display:none">
                                   <td>
                         
                    <table border="0" cellspacing="0" cellpadding="0" width="100%">
                         <tr valign="bottom">
                              <td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td><td width="18"><img src="../images/line.gif" width="18" height="16"></td><td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td>
                              <td width="31"><img src="../images/docjoin.gif" width="31" height="16" border="0"></td>
                              <td nowrap class="node">&nbsp;NAILS (ID: 263)</td>
                         </tr>
                    </table>

                    <table border="0" cellspacing="0" cellpadding="0" width="100%">
                         <tr valign="bottom">
                              <td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td><td width="18"><img src="../images/line.gif" width="18" height="16"></td><td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td>
                              <td width="31"><img src="../images/doc.gif" width="31" height="16" border="0"></td>
                              <td nowrap class="node">&nbsp;LUMBER (ID: 264)</td>
                         </tr>
                    </table>
     
                                   </td>
                              </tr>
                         </table>
                             
                                   </td>
                              </tr>
                         </table>
                         
                    <table border="0" cellspacing="0" cellpadding="0" width="100%">
                         <tr valign="bottom">
                              <td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td>
                              <td width="31"><img class="LEVEL9" src="../images/folderclosed.gif" id="9" width="31" height="16" border="0"></td>
                              <td nowrap class="node">&nbsp;DESK (ID: 267)</td>
                         </tr>

                    </table>
                   
                         <table border="0" cellspacing="0" cellpadding="0" width="100%">
                              <tr valign="bottom" class="LEVEL10" id="10" style="display:none">
                                   <td>
                         
                    <table border="0" cellspacing="0" cellpadding="0" width="100%">
                         <tr valign="bottom">
                              <td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td><td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td>
                              <td width="31"><img class="LEVEL11" src="../images/folderclosed.gif" id="11" width="31" height="16" border="0"></td>
                              <td nowrap class="node">&nbsp;BUILDING MATERIALS (ID: 268)</td>
                         </tr>

                    </table>
                   
                         <table border="0" cellspacing="0" cellpadding="0" width="100%">
                              <tr valign="bottom" class="LEVEL12" id="12" style="display:none">
                                   <td>
                         
                    <table border="0" cellspacing="0" cellpadding="0" width="100%">
                         <tr valign="bottom">
                              <td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td><td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td><td width="18"><img src="../images/line.gif" width="18" height="16"></td>
                              <td width="31"><img src="../images/docjoin.gif" width="31" height="16" border="0"></td>
                              <td nowrap class="node">&nbsp;LUMBER (ID: 271)</td>
                         </tr>
                    </table>

                    <table border="0" cellspacing="0" cellpadding="0" width="100%">
                         <tr valign="bottom">
                              <td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td><td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td><td width="18"><img src="../images/line.gif" width="18" height="16"></td>
                              <td width="31"><img src="../images/doc.gif" width="31" height="16" border="0"></td>
                              <td nowrap class="node">&nbsp;HARDWARE (ID: 273)</td>
                         </tr>
                    </table>
     
                                   </td>
                              </tr>
                         </table>
                         
                    <table border="0" cellspacing="0" cellpadding="0" width="100%">
                         <tr valign="bottom">
                              <td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td><td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td>
                              <td width="31"><img class="LEVEL13" src="../images/folderclosed.gif" id="13" width="31" height="16" border="0"></td>
                              <td nowrap class="node">&nbsp;EQUIPMENT (ID: 269)</td>
                         </tr>

                    </table>
                   
                         <table border="0" cellspacing="0" cellpadding="0" width="100%">
                              <tr valign="bottom" class="LEVEL14" id="14" style="display:none">
                                   <td>
                         
                    <table border="0" cellspacing="0" cellpadding="0" width="100%">
                         <tr valign="bottom">
                              <td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td><td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td><td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td>
                              <td width="31"><img src="../images/docjoin.gif" width="31" height="16" border="0"></td>
                              <td nowrap class="node">&nbsp;COMPUTER (ID: 270)</td>
                         </tr>
                    </table>

                    <table border="0" cellspacing="0" cellpadding="0" width="100%">
                         <tr valign="bottom">
                              <td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td><td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td><td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td>
                              <td width="31"><img src="../images/docjoin.gif" width="31" height="16" border="0"></td>
                              <td nowrap class="node">&nbsp;MONITOR (ID: 272)</td>
                         </tr>
                    </table>

                    <table border="0" cellspacing="0" cellpadding="0" width="100%">
                         <tr valign="bottom">
                              <td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td><td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td><td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td>
                              <td width="31"><img src="../images/docjoin.gif" width="31" height="16" border="0"></td>
                              <td nowrap class="node">&nbsp;TELEPHONE (ID: 274)</td>
                         </tr>
                    </table>

                    <table border="0" cellspacing="0" cellpadding="0" width="100%">
                         <tr valign="bottom">
                              <td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td><td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td><td width='20'><img src="../images/pixel.gif" width='20' height="1" border="0"></td>
                              <td width="31"><img src="../images/doc.gif" width="31" height="16" border="0"></td>
                              <td nowrap class="node">&nbsp;CHAIR (ID: 275)</td>
                         </tr>
                    </table>
     
                                   </td>
                              </tr>
                         </table>
                             
                                   </td>
                              </tr>
                         </table>
                             
                                   </td>
                              </tr>
                         </table>
                         
                         </td>
                    </tr>
               </table>
<SCRIPT LANGUAGE="Javascript">
     <!--
          //These two arrays work with each other to identify the menu element that should
          //be hidden or made visible.  There is a one-to-one relationship between
          //the rows of each array.  For example arClickedElementID(0) contains the
          //ID to access the element ID stored in arAffectedMenuItemID(0).
               
          //Note: The value of the ASP variable iTotal used below represents the
          //total number of items and subitems in the menu.  The value is set by
          //reference in the DisplayNode() subroutine call
          var arClickedElementID = new Array( "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15");
          var arAffectedMenuItemID = new Array( "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16");
               
          //This function queries the arClickedElementID[] and arAffectedMenuItemID[] arrays
          //to get an object reference to the appropriate menu element to show or hide.
          function fnLookupElementRef(sID)
          {
               var i;
               for (i=0;i<arClickedElementID.length;i++)
                    if (arClickedElementID[i] == sID)
                         return document.all(arAffectedMenuItemID[i]);
                             
               return null;
          }
               
          //This function is responsible for showing/hiding the menu items.  It
          //also switches the images accordingly
          function doChangeTree()
          {
               var targetID, srcElement, targetElement;
               srcElement = window.event.srcElement;
                   
               //Only work with elements that have LEVEL in the classname
               if(srcElement.className.substr(0,5) == "LEVEL")
               {
                    //Using the ID of the item that was clicked, we look up
                    //and retrieve an object reference to the menu item that
                    //should be shown or hidden
                    targetElement = fnLookupElementRef(srcElement.id)          
                         
                    if (targetElement != null)
                    {
                         //First find out if the current folder is empty
                         //We find out based on the name of the image used
                         var sImageSource = srcElement.src;
                         if (sImageSource.indexOf("empty") == -1)
                         {
                              if (targetElement.style.display == "none")
                              {
                                   //Our menu item is currently hidden, so display it
                                   targetElement.style.display = "";
                                       
                                   if (srcElement.className == "LEVEL1")
                                        //Set a special open-folder graphic for the root folder
                                        srcElement.src = "../images/minusonly.gif";
                                   else
                                        //Otherwise, just show the open folder icon
                                        srcElement.src = "../images/folderopen.gif";
                              }
                              else
                              {
                                   //Our menu item is currently visible, so hide it
                                   targetElement.style.display = "none";
                                       
                                   if (srcElement.className == "LEVEL1")
                                        //Set a special closed-folder graphic for the root folder
                                        srcElement.src = "../images/plusonly.gif";
                                   else
                                        //Otherwise, just show the closed folder icon
                                        srcElement.src = "../images/folderclosed.gif";
                              }
                         }
                    }
               }
          }    
               
          //Capture user clicks on the web page and
          //call the doChangeTree() function to
          //handle the event
          document.onclick = doChangeTree;
     -->
     </SCRIPT>

     </body>
</html>Start Free Trial
[+][-]02.02.2005 at 04:21PM PST, ID: 13209538

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: JavaScript
Tags: expand, javascript, all, contract
Sign Up Now!
Solution Provided By: dakyd
Participating Experts: 2
Solution Grade: A
 
 
[+][-]02.03.2005 at 07:01AM PST, ID: 13214904

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.03.2005 at 08:04AM PST, ID: 13215689

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32