Exforsys.com
 

Sponsored Links

 

jQuery Tutorials

 
Home Tutorials jQuery
 

jQuery Three-color Alternating Pattern

 

Learning jQuery - Three-color Alternating Pattern

There may be times when we want to apply more complex striping. For example, we can apply a pattern of three alternating row colors rather than just two. To do so, we first need to define another CSS rule for the third row. We'll also reuse the odd and even styles for the other two, but add more appropriate class names for them:



Sample Code
  1. tr.even,
  2. tr.first {
  3. background-color: #eee
  4. }
  5. tr.odd,
  6. tr.second {
  7. background-color: #ddd
  8. }
  9. tr.third {
  10. background-color: #ccc
  11. }
Copyright exforsys.com



To apply this pattern, we start the same way as the previous example—by selecting all rows that are descendants of a , but filtering out the rows that contain a   . This time, however, we attach the .each() method so that we can use its built-in index:


Sample Code
  1. $(document).ready(function() {
  2. $('table.striped tbody tr').not('[th]').each(function(index) {
  3. //Code to be applied to each element in the matched set.
  4. })
  5. })
Copyright exforsys.com



To make use of the index, we can assign our three classes to a numeric key: 0, 1, or 2. We'll do this by creating an object, or map:


Sample Code
  1. $(document).ready(function() {
  2. var classNames = {
  3. 0: 'first',
  4. 1: 'second',
  5. 2: 'third'
  6. }
  7. $('table.striped tbody tr').not('[th]').each(function(index) {
  8. // Code to be applied to each element in the matched set.
  9. })
  10. })
Copyright exforsys.com



Finally, we need to add the class that corresponds to those three numbers, sequentially, and then repeat the sequence. The modulus operator, designated by a %, is especially convenient for such calculations. A modulus returns the remainder of one number divided by another. This modulus, or remainder value, will always range between 0 and one less than the dividend. Using 3 as an example, we can see this pattern:


3/3 = 1, remainder 0.
4/3 = 1, remainder 1.
5/3 = 1, remainder 2.
6/3 = 2, remainder 0.
7/3 = 2, remainder 1.
8/3 = 3, remainder 2.


And so on. Since we want the remainder range to be 0 – 2, we can use 3 as the divisor (second number) and the value of index as the dividend (first number). Now we simply put that calculation in square brackets after classNames to retrieve the corresponding class from the object variable as the .each() method steps through the matched set of rows:


Sample Code
  1. $(document).ready(function() {
  2. var classNames = {
  3. 0: 'first',
  4. 1: 'second',
  5. 2: 'third'
  6. }
  7. $('table.striped tbody tr').not('[th]').each(function(index) {
  8. $(this).addClass(classNames[index % 3])
  9. })
  10. })
Copyright exforsys.com




With this code in place, we now have the table striped with three alternating background colors:




We could of course extend this pattern to four, five, six, or more background colors by adding key-value pairs to the object variable and increasing the value of the divisor in classNames[index % n].



Read Next: jQuery Alternating Triplets



 

 

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