alt
Sponsored links
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow jQuery arrow jQuery Interacting with Other Code
Site Search


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:

  1. $(document).ready(function() {
  2.  
  3. $('table.filterable').each(function() {
  4.  
  5. var $table = $(this);
  6.  
  7. $table.find('th').each(function (column) {
  8.  
  9. if ($(this).is('.filter-column')) {
  10.  
  11. var $filters = $('</p>
  12. <div class="filters">
  13. <h3>Filter by ' +
  14.  
  15. $(this).text() + ':</h3>
  16. </div>
  17. ');
  18.  
  19. var keywords = {};
  20.  
  21. $table.find('tbody tr td').filter(':nth-child(' + (column +
  22.  
  23. 1) + ')').each(function() {
  24.  
  25. keywords[$(this).text()] = $(this).text();
  26.  
  27. });
  28.  
  29. $('
  30. <div class="filter">all</div>
  31. ').click(function() {
  32.  
  33. $table.find('tbody tr').show().removeClass('filtered');
  34.  
  35. $(this).addClass('active').siblings().removeClass('active');
  36.  
  37. $table.trigger('stripe');
  38.  
  39. }).addClass('clickable active').appendTo($filters);
  40.  
  41. $.each(keywords, function (index, keyword) {
  42.  
  43. $('
  44. <div class="filter"></div>
  45. ').text(keyword).bind('click',
  46.  
  47. {'keyword': keyword}, function(event) {
  48.  
  49. $table.find('tbody tr').each(function() {
  50.  
  51. if ($('td', this).filter(':nth-child(' + (column + 1) +
  52.  
  53. ')').text() == event.data['keyword']) {
  54.  
  55. $(this).show().removeClass('filtered');
  56.  
  57. }
  58.  
  59. else if ($('th',this).length == 0) {
  60.  
  61. $(this).hide().addClass('filtered');
  62.  
  63. }
  64.  
  65. });
  66.  
  67. $(this).addClass('active').siblings().removeClass('active');
  68.  
  69. $table.trigger('stripe');
  70.  
  71. }).addClass('clickable').appendTo($filters);
  72.  
  73. });
  74.  
  75. $filters.insertBefore($table);
  76.  
  77. }
  78.  
  79. });
  80.  
  81. });
  82.  
  83. });
 

 

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:

  1. $(document).ready(function() {
  2.  
  3. $('table.striped').each(function() {
  4.  
  5. $(this).bind('stripe', function() {
  6.  
  7. var rowIndex = 0;
  8.  
  9. $('tbody tr:not(.filtered)', this).each(function(index) {
  10.  
  11. if ($('th',this).length) {
  12.  
  13. $(this).addClass('subhead');
  14.  
  15. rowIndex = -1;
  16.  
  17. } else {
  18.  
  19. if (rowIndex % 6 < 3) {
  20.  
  21. $(this).removeClass('odd').addClass('even');
  22.  
  23. }
  24.  
  25. else {
  26.  
  27. $(this).removeClass('even').addClass('odd');
  28.  
  29. }
  30.  
  31. };
  32.  
  33. rowIndex++;
  34.  
  35. });
  36.  
  37. });
  38.  
  39. $(this).trigger('stripe');
  40.  
  41. });
  42.  
  43. });
 

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:


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