Exforsys.com
 

Sponsored Links

 

jQuery Tutorials

 
Home Tutorials jQuery
 

jQuery Expanding and Collapsing Table Rows

 

jQuery Expanding and Collapsing Table Rows

The expanding and collapsing behavior added earlier also conflicts with our filters. If a section is collapsed and a new filter is chosen, then the matching items are displayed, even if in the collapsed section. Conversely, if the table is filtered and a section is expanded, then all items in the expanded section are displayed regardless of whether they match the filter.



Since we have added the filtered class to all rows when they are removed by a filter button, we can check for this class inside our collapser's click handler:


Sample Code
  1. var toggleSrc = $(this).attr('src')
  2. if ( toggleSrc == toggleMinus ) {
  3. $(this).attr('src', togglePlus)
  4. .parents('tr').siblings().addClass('collapsed').fadeOut('fast')
  5. } else{
  6. $(this).attr('src', toggleMinus)
  7. .parents('tr').siblings().removeClass('collapsed')
  8. .not('.filtered').fadeIn('fast')
  9. }
Copyright exforsys.com



While we are collapsing or expanding rows, we add or remove another new class on the rows. We need this class to solve the other half of the problem. The filtering code can use the class to ensure that a row should be shown when the filter changes:


Sample Code
  1. $table.find('tbody tr').each(function() {
  2. if ($('td', this).filter(':nth-child(' + (column + 1) + ')').text()
  3. == e.data['keyword']) {
  4. $(this).removeClass('filtered').not('.collapsed').show()
  5. }
  6. else if ($('th',this).length == 0) {
  7. $(this).addClass('filtered').hide()
  8. }
  9. })
Copyright exforsys.com




Now our features play nicely, each able to hide and show the rows independently.



Read Next: jQuery Table Row Finished Code



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape