alt
Advertisement
Sponsored links
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow jQuery arrow jQuery Collapsing and Expanding Table Rows
Site Search


jQuery Collapsing and Expanding Table Rows

jQuery Collapsing and Expanding Table Rows

When large sets of data are grouped in tables, as each year's set of articles are in our News page, collapsing, or hiding, a section's contents can be a convenient way to get a broad view of all of the table's data without having to scroll so much.

To make the sections of the news article table collapsible, we first prepend a minus-symbol image to each subheading row's first cell. The image is inserted with JavaScript, because if JavaScript is not available for the row collapsing, the image might confuse those who expect clicking on it to actually trigger some kind of event:

  1. $(document).ready(function() {
  2.  
  3. var toggleMinus = '../icons/bullet_toggle_minus.png';
  4.  
  5. var togglePlus = '../icons/bullet_toggle_plus.png';
  6.  
  7. var $subHead = $('tbody th:first-child');
  8.  
  9. $subHead.prepend('<img src="' + toggleMinus + '"
  10. alt="collapse this section" />');
  11.  
  12. });
 

Note that we set variables for the location of both a minus-symbol and a plus-symbol image. This way we can change the image's src attribute when the image is clicked and the rows are collapsed or expanded.
Next we use the .addClass() method to make the newly created images appear clickable:

  1. $(document).ready(function() {
  2.  
  3. var toggleMinus = '../icons/bullet_toggle_minus.png';
  4.  
  5. var togglePlus = '../icons/bullet_toggle_plus.png';
  6.  
  7. var $subHead = $('tbody th:first-child');
  8.  
  9. $subHead.prepend('<img src="' + toggleMinus + '"
  10. alt="collapse this section" />');
  11.  
  12. $('img', $subHead).addClass('clickable');
  13.  
  14. });
 

Finally, we can add code inside a .click() method to do the collapsing and expanding. A condition will check the current value of the clicked image's src attribute. If it equals the file path represented by the toggleMinus variable, then all of the other <tr> elements within the same <tbody> will be hidden, and the src attribute will be set to the value of the togglePlus variable. Otherwise, these <tr> elements will be shown and the src will change back to the value of toggleMinus:

  1. $(document).ready(function() {
  2.  
  3. var toggleMinus = '../icons/bullet_toggle_minus.png';
  4.  
  5. var togglePlus = '../icons/bullet_toggle_plus.png';
  6.  
  7. var $subHead = $('tbody th:first-child');
  8.  
  9. $subHead.prepend('<img src="' + toggleMinus + '"
  10. alt="collapse this section" />');
  11.  
  12. $('img', $subHead).addClass('clickable')
  13.  
  14. .click(function() {
  15.  
  16. var toggleSrc = $(this).attr('src');
  17.  
  18. if ( toggleSrc == toggleMinus ) {
  19.  
  20. $(this).attr('src', togglePlus)
  21.  
  22. .parents('tr').siblings().fadeOut('fast');
  23.  
  24. } else{
  25.  
  26. $(this).attr('src', toggleMinus)
  27.  
  28. .parents('tr').siblings().fadeIn('fast');
  29.  
  30. };
  31.  
  32. });
  33.  
  34. })
 

With this code in place, clicking on the minus-symbol image next to 2007 makes the table look like this:

The 2007 news articles aren't removed; they are just hidden until we click the plus-symbol image that now appears in that row.

Table rows present particular obstacles to animation, since browsers use different values (table-row and block) for their visible display property. The .hide() and .show() methods, without animation, are always safe to use with table rows. As of jQuery version 1.1.3, .fadeIn() and .fadeOut() can be used as well.


Trackback(0)
Comments (0)add comment

Write comment

busy
 
< Prev   Next >
Exforsys Offers
© 2008 Exforsys.com
Joomla! is Free Software released under the GNU/GPL License.
Page copy protected against web site content infringement by Copyscape