Exforsys.com
 

Sponsored Links

 

jQuery Tutorials

 
Home Tutorials jQuery
 

jQuery Interacting with Other Code

 

jQuery Interacting with Other Code

We learned with our sorting and paging code that we can't treat the various features we write as islands. The behaviors we build can interact in sometimes surprising ways; for this reason, it is worth revisiting our earlier efforts to examine how they coexist with the new filtering capabilities we have added.



Row Striping

The advanced row striping we put in place earlier is confused by our new filters. Since the tables are not re-striped after a filter is performed, rows retain their coloring as if the filtered rows were still present.

To account for the filtered rows, the striping code needs to be able to find them. We can add a class on the rows when they are filtered:


Sample Code
  1. $(document).ready(function() {
  2. $('table.filterable').each(function() {
  3. var $table = $(this)
  4. $table.find('th').each(function (column) {
  5. if ($(this).is('.filter-column')) {
  6. var $filters = $('</p><br>
  7. <div class="filters">
  8. <h3>Filter by ' +
  9. $(this).text() + ':</h3>
  10. </div>
  11. ')
  12. var keywords = {}
  13. $table.find('tbody tr td').filter(':nth-child(' + (column +
  14. 1) + ')').each(function() {
  15. keywords[$(this).text()] = $(this).text()
  16. })
  17. $('
  18. <div class="filter">all</div>
  19. ').click(function() {
  20. $table.find('tbody tr').show().removeClass('filtered')
  21. $(this).addClass('active').siblings().removeClass('active')
  22. $table.trigger('stripe')
  23. }).addClass('clickable active').appendTo($filters)
  24. $.each(keywords, function (index, keyword) {
  25. $('
  26. <div class="filter"></div>
  27. ').text(keyword).bind('click',
  28. {'keyword': keyword}, function(event) {
  29. $table.find('tbody tr').each(function() {
  30. if ($('td', this).filter(':nth-child(' + (column + 1) +
  31. ')').text() == event.data['keyword']) {
  32. $(this).show().removeClass('filtered')
  33. }
  34. else if ($('th',this).length == 0) {
  35. $(this).hide().addClass('filtered')
  36. }
  37. })
  38. $(this).addClass('active').siblings().removeClass('active')
  39. $table.trigger('stripe')
  40. }).addClass('clickable').appendTo($filters)
  41. })
  42. $filters.insertBefore($table)
  43. }
  44. })
  45. })
  46. })
Copyright exforsys.com


 


Whenever the current filter changes, we trigger the stripe event. This uses the same trick we implemented when making our pager aware of sorting—adding a new custom event. We have to rewrite the striping code to define this event:


Sample Code
  1. $(document).ready(function() {
  2. $('table.striped').each(function() {
  3. $(this).bind('stripe', function() {
  4. var rowIndex = 0
  5. $('tbody tr:not(.filtered)', this).each(function(index) {
  6. if ($('th',this).length) {
  7. $(this).addClass('subhead')
  8. rowIndex = -1
  9. } else {
  10. if (rowIndex % 6 < 3) {
  11. $(this).removeClass('odd').addClass('even')
  12. }
  13. else {
  14. $(this).removeClass('even').addClass('odd')
  15. }
  16. }
  17. rowIndex++
  18. })
  19. })
  20. $(this).trigger('stripe')
  21. })
  22. })
Copyright exforsys.com




The selector to find table rows now skips filtered rows. We also must remove obsolete classes from rows, as this code may now be executed multiple times. With both the new event handler and its triggers in place, the filtering operation respects row striping:




Read Next: jQuery Expanding and Collapsing Table Rows



 

 

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