Exforsys.com
 

Sponsored Links

 

jQuery Tutorials

 
Home Tutorials jQuery
 

jQuery Alternating Triplets

 

Learning jQuery - Alternating Triplets

Suppose we want to use two colors, but have each one display three rows at a time. For this, we can employ the odd and even classes again, as well as the modulus operator. But we'll also reset the class each time we're presented with a row containing <th> elements.



If we don't reset the alternating row class, we may be faced with unexpected colors after the first group of rows is striped. So far, our example table has avoided such problems because the first group consists of 12 rows, which, conveniently, is divisible by both 2 and 3. For the triplet striping scenario, we'll remove two rows, leaving us with 10 in the first group, to emphasize the class resetting.


We begin this striping technique by setting two variables, rowClass and rowIndex. We'll use the .each() method this time as well, but rather than relying on the built-in index, we'll use a custom rowIndex variable so that we can reset it on the rows with <th>:


Sample Code
  1. $(document).ready(function() {
  2. var rowClass = 'even'
  3. var rowIndex = 0
  4. $('table.striped tbody tr').each(function(index) {
  5. $(this).addClass(rowClass)
  6. })
  7. })
Copyright exforsys.com



Notice that since we have removed the :not([th]) selector, we'll have to account for those subheading rows within the .each(). But first, let's get the triplet alternation working properly. So far, every <tr> will become <tr class="even">. For each row, we can check to see if the rowIndex % 3 equals 0. If it does, we toggle the value of rowClass. Then we increment the value of rowIndex:


Sample Code
  1. $(document).ready(function() {
  2. var rowClass = 'even'
  3. var rowIndex = 0
  4. $('table.striped tbody tr').each(function(index) {
  5. if (rowIndex % 3 == 0) {
  6. rowClass = (rowClass == 'even' ? 'odd' : 'even')
  7. }
  8. $(this).addClass(rowClass)
  9. rowIndex++
  10. })
  11. })
Copyright exforsys.com




A ternary, or conditional, operator is used to set the changed value of rowClass because of its succinctness. That single line could be rewritten as:


Sample Code
  1. if (rowClass == 'even') {
  2. rowClass = 'odd'
  3. } else {
  4. rowClass = 'even'
  5. }
Copyright exforsys.com



In either case, the code now produces table striping that looks like this:




Perhaps surprisingly, the subheading rows have retained their proper formatting. But let's not be fooled by appearances. The 2007 subheading row is now set in the HTML as <tr class="odd"> and the 2006 row has <tr class="even">. In the stylesheet, however, the greater specificity of the element's rule outweighs that of the two classes:


Sample Code
  1. #content tbody th {
  2. background-color: #6f93ce
  3. padding-left: 6px
  4. }
  5. tr.even {
  6. background-color: #eee
  7. }
  8. tr.odd {
  9. background-color: #ddd
  10. }
Copyright exforsys.com



Nevertheless, because the rowIndex numbering does not account for these subheading rows, we have mis-classed rows from the start; this is evident because the first striping color change occurs after two rows rather than three.


We need to include another condition, checking if the current row contains a <th>. If it does, we'll set the value of rowClass to subhead and set rowIndex to -1:


Sample Code
  1. $(document).ready(function() {
  2. var rowClass = 'even'
  3. var rowIndex = 0
  4. $('table.striped tbody tr').each(function(index) {
  5. if ($('th', this).length) {
  6. rowClass = 'subhead'
  7. rowIndex = -1
  8. } else if (rowIndex % 3 == 0) {
  9. rowClass = (rowClass == 'even' ? 'odd' : 'even')
  10. }
  11. $(this).addClass(rowClass)
  12. rowIndex++
  13. })
  14. })
Copyright exforsys.com



With rowIndex at -1 for the subheading rows, the variable will be incremented to 0 for the next row—precisely where we want it to start for each group of striped rows. Now we can see the striping with each year's articles beginning with three light colored rows and alternating three at a time between lighter and darker:



A final note about this striping code—while the ternary operator is indeed concise, it can get confusing when the conditions get more complex. The sophisticated striping variations can be more easily managed by using basic if-else conditions instead:



Sample Code
  1. $(document).ready(function() {
  2. var rowIndex = 0
  3. $('tbody tr').each(function(index) {
  4. if ($('th',this).length) {
  5. $(this).addClass('subhead')
  6. rowIndex = -1
  7. } else {
  8. if (rowIndex % 6 < 3) {
  9. $(this).addClass('even')
  10. }
  11. else {
  12. $(this).addClass('odd')
  13. }
  14. }
  15. rowIndex++
  16. })
  17. })
Copyright exforsys.com




Now we've achieved the same effect as before, but also made it easier to include additional else if conditions.



Read Next: jQuery Row Highlighting



 

 

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 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape