Understanding the code's logic
This section describes the logic behind the code that makes colors alternate in a repeated region. For simplicity, it assumes your repeated region is applied to an HTML table row (though the logic can be applied to almost any page element that can be repeated).
The background color of a normal table row is specified in the <tr> tag. For example, the following HTML line sets the row's background color to light gray:
With a repeated region, you write only the first <tr> tag; the application server writes the other <tr> tags to create the required number of rows to accommodate the number of records to display. Your job is to get the application server to insert the bgcolor attribute in every second tag it writes.
You can do this by writing a small server-side script telling the application server to insert the bgcolor attribute when the following condition is true: the row number in the recordset is odd (or even, depending on your preference).
One way to determine whether the row number is odd or even is to divide the number by 2 and check the remainder. If the remainder is 1, the number must be odd; if the remainder is 0, the number must be even.
In scripting languages, you get the remainder of an integer division with the modulus operator. For example, the modulus of 18 divided by 7 is 4 (7 times 2, with 4 remaining). Here's how to obtain the remainder of a division in various scripting languages supported by UltraDev:
|
In ColdFusion, the keyword MOD is used as the modulus operator. Example: |
|
remainder = number1 MOD number2
|
|
In VBScript, the keyword Mod is used as the modulus operator. Example: |
|
remainder = number1 Mod number2
|
|
In JavaScript and Java, the percentage symbol (% ) is used as the modulus operator. Example: |
|
remainder = number1 % number2;
|
Determining whether the current recordset row number is odd or even is the basis of the code that makes colors alternate in a repeated region. The following sections provide the specifics:
Writing the ColdFusion code
Writing the ASP code in VBScript
Writing the ASP code in JavaScript
Writing the JSP code
|