Avg. Rating 5.0

Problem

When displaying data you need to alternate the background colour of the rows to differentiate them.

Solution

Use an incremented variable or currentrow (for query objects) and % (MOD) the value with 2.

Detailed explanation

<cfset i = 0>

<td class="#IIF(i%2, DE("style1"), DE("style2"))#">

      #rowdata#

</td>

<cfset i++>

% (MODULUS) returns the remainder of a division, so anything divisible by 2 (even numbers) will return 0 (false), any odd numbers will return a remainder (not 0 - true) and you can use this to alternate rows. You could extend this to every 2 rows to be alternated by changing i%2 to i%3 in the above example.


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes