A great tutorial that is found in many places, but I thought I'd also add to our listings is the alternating row color tutorial. What is this you ask?

Well, this tutorial demonstrates how to alternate colors in a table when you are displaying records. Take a look at the two examples below, which do you think looks nicer and easier to read?

This is a table of fake users:
1 John Smith
2 Jon Doe
3 Jane Doe
4 Dennis the Menace
5 John Wayne
(Without alternating row colors)
This is a table of fake users:
1 John Smith
2 John Doe
3 Jane Doe
4 Dennis the Menace
5 John Wayne
(With alternating row colors)

Now you can get this effect very easily and very fast! :)

Let me show you how:

The first thing you'll need to do is to create your usual query. Let's make a fake query now:

  <cfquery name="qMyQuery" datasource="myDSN">
      SELECT     *
      FROM        MyTable
      ORDER BY  FieldName
   </cfquery>

Ok, now comes the alternating row color trick:

	<table border="0" width="100%">
		<tr>
  			<td width="100%" bgcolor="#808080" colspan="2">
 				<font size="2" face="Verdana"><b>This is a table of fake users:</b></font>
            </td>
        </tr>
        <cfoutput query="qMyQuery">
        <tr bgcolor="###iif(currentrow MOD 2,DE('ffffff'),DE('efefef'))#">
        	<td width="4%"><font size="2" face="Verdana">1</font></td>
  			<td width="96%"><font size="2" face="Verdana">John Smith</font></td>
        </tr>
       	</cfoutput>
  </table>

Now the magic code takes place in the following code: "###iif(currentrow MOD 2,DE('ffffff'),DE('efefef'))#" text above. It's that simple, that's all you need to alternate row colors!

Questions? Comments? Email me: webmaster@easycfm.com

About This Tutorial
Author: Pablo Varando
Skill Level: Beginner 
 
 
 
Platforms Tested: CF5,CFMX,CF8,CF9,BlueDragon,Railo
Total Views: 181,878
Submission Date: September 17, 2002
Last Update Date: August 25, 2011
All Tutorials By This Autor: 47
Discuss This Tutorial
  • I have been using a longer version code for year, your version is much better, thanks. www.hotelsoftware.my

Advertisement

Sponsored By...
Healing Touch Massage - Dripping Springs, Texas - Deep Tissue Massage and Swedish Massage Services just $39 for a 50 minute massage!