Greg's Blog

helping me remember what I figure out

Generating Dynamic Tables With Varying Row Colours

| Comments

Generating dynamic tables with varying row colours

I always wanted to know how in Coldfusion administrator the rows in a table were alternating in colour. Well I came across this article by Adam Maloney on Defusion (http://www.defusion.com/) [Article 54]

This trick is relatively simple. Using the query.currentrow variable, and some simple math, you can have a nifty table:

<CFOUTPUT QUERY="myquery">
  <CFIF currentrow mod 2>
    <TR color="color1"...></TR>
  <CFELSE>
    <TR color="color2"...></TR>
  </CFIF>
</CFOUTPUT>

The if statement is the key. The expression currentrow mod 2 returns the modulus (remainder division) of the current query and row 2. If the return value is 0, then currentrow is even. If it is non-zero, then currentrow is odd.