How do I alternate row colors in a table?

Two functions are used in one expression which allow for a clean implementation of alternating row colors. IIf() takes three parameters (condition,expression1,expression2) and evaluates either the first expression or the second expression depending if the current row is even or odd. DE() takes on parameter which is an expression and delays the evaluation of the expressions.

The following example uses CSS to style the alternating rows of content:

<cfquery name="myTest" datasource="testDatasource">
	SELECT *
	FROM table
</cfquery>
<style type="text/css">
   tr.lightrow td {
      background-color: #FFFFFF;
   }
   tr.darkrow td {
      background-color: #EFEFEF;
   }
</style>
<table>
<cfoutput query="myTest">
	<tr class="#iif(currentrow MOD 2,DE('lightrow'),DE('darkrow'))#">
		<td>1</td>
		<td>myField</td>
	</tr>
</cfoutput>
</table>

This question was written by Cory Toth
It was last updated on January 9, 2006.

Categories

Display and Layout

Comments

comments powered by Disqus