jQuery Treeview – Expand and Collapse All Without the TreeControl

Posted by Ben Griswold on Johnny Coder See other posts from Johnny Coder or by Ben Griswold
Published on Fri, 09 Apr 2010 01:27:45 +0000 Indexed on 2010/04/09 1:43 UTC
Read the original article Hit count: 712

Filed under:
|

The jQuery Treeview Plugin provides collapse all, expand all and toggle all support with very little effort on your part.

Simply add a treecontrol with three links, and the treeview, to your page…  

  1. <div id="treecontrol">
  2.     <a title="Collapse the entire tree below" href="#"><img src="../images/minus.gif" /> Collapse All</a>
  3.     <a title="Expand the entire tree below" href="#"><img src="../images/plus.gif" /> Expand All</a>
  4.     <a title="Toggle the tree below, opening closed branches, closing open branches" href="#">Toggle All</a>
  5. </div>
  6. <ul id="treeview" class="treeview-black">
  7.     <li>Item 1</li>
  8.     <li>
  9.         <span>Item 2</span>
  10.         <ul>
  11.             <li>
  12.                 <span>Item 2.1</span>
  13.  
  14.                 <ul>
  15.                     <li>Item 2.1.1</li>
  16.                     <li>Item 2.1.2</li>
  17.                 </ul>
  18.             </li>
  19.             <li>Item 2.2</li>
  20.             <li class="closed">
  21.  
  22.                 <span>Item 2.3 (closed at start)</span>
  23.                 <ul>
  24.                     <li>Item 2.3.1</li>
  25.                     <li>Item 2.3.2</li>
  26.                 </ul>
  27.             </li>
  28.         </ul>
  29.     </li>
  30. </ul>

…and then associate the control to the treeview when defining the treeview settings.

  1. $("#treeview").treeview({
  2.     control: "#treecontrol",
  3.     persist: "cookie",
  4.     cookieId: "treeview-black"
  5. });

It really couldn’t be easier and it works great!

image

But the plugin doesn’t offer a lot of flexibility when it comes to layout.  For example, the plugin assumes your treecontrol will include links.  If you want buttons or images or whatever, you are out of luck.  The plugin also assumes a set number of links and the collapse all handler is associated with the first link inside of the treecontrol, a:eq(0), the expand all handler is associated with the second link and so on.  So you really can’t incorporate the toggle all link by itself unless you manually hide the other options.

Which brings me to the point of this post – making the collapse/expand/toggle layout more flexible without modifying the plugin’s source code. We will continue to use the treecontrol actions but we’re not going to use them directly. In fact, our custom collapse, expand and toggle links will trigger the actions for us.  So, hide the treecontrol links and associate the treecontrol click events with the click events of other controls.  Finally, render the treeview with the same setting definitions as usual.

  1. $(document).ready(function() {
  2.     // The plugin shows the treecontrol after the
  3.     // collapse, expand and toggle events are hooked up
  4.     // Just hide the links.
  5.     $('#treecontrol a').hide();
  6.  
  7.     // On click of your custom links, button, etc
  8.     // Trigger the appropriate treecontrol click
  9.     $('#expandAll').click(function() {        
  10.         $('#treecontrol a:eq(1)').click();    
  11.     });
  12.     
  13.     $('#collapseAll').click(function() {
  14.         $('#treecontrol a:eq(0)').click();        
  15.     });
  16.  
  17.     // Render the treeview per usual.    
  18.     $("#treeview").treeview({
  19.         control: "#treecontrol",
  20.         persist: "cookie",
  21.         cookieId: "treeview-black"
  22.     });
  23. });

Since I’m not using the treecontrol directly, I move it to the bottom of the page but it doesn’t really matter where the treecontrol goes.

  1. <div>
  2.     <a id="collapseAll" href="#">Collapse All Outside of TreeControl</a>
  3. </div>
  4.  
  5. <ul id="treeview" class="treeview-black">
  6.     <li>Item 1</li>
  7.     <li>
  8.         <span>Item 2</span>
  9.         <ul>
  10.             <li>
  11.                 <span>Item 2.1</span>
  12.                 <ul>
  13.                     <li>Item 2.1.1</li>
  14.                     <li>Item 2.1.2</li>
  15.                 </ul>
  16.             </li>
  17.             <li>Item 2.2</li>
  18.             <li class="closed">
  19.                 <span>Item 2.3 (closed at start)</span>
  20.                 <ul>
  21.                     <li>Item 2.3.1</li>
  22.                     <li>Item 2.3.2</li>
  23.                 </ul>
  24.             </li>
  25.         </ul>
  26.     </li>
  27. </ul>
  28.  
  29. <div>
  30.     <input type="button" id="expandAll" value="Expand All Outside of TreeControl"/>
  31. </div>
  32.  
  33. <div id="treecontrol">
  34.     <a href="#"></a><a href="#"></a><a href="#"></a>
  35. </div>

The above jQuery and Html snippets generate the following ugly output which shows the separated collapse/expand elements. If you want the toggle all option, I bet you can figure out how to put it in place.  I’ve made the source available below if you’re interested.

image

© Johnny Coder or respective owner

Related posts about controls

Related posts about jQuery