Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shading every other row in a table

Status
Not open for further replies.

jmgibson

Technical User
Oct 1, 2002
81
US
When I have a table generate dynamically (via coldfusion), how can I shade every other row. Is this done through javascript or is it a little simpler than that?
 
I don't know coldfusion at all, but this is how I do it in ASP:

I create 2 different styles for my TD's, each with a different color.

I create a boolean that I place initially on 'TRUE'

In the loop script to create the different rows of my table I place a check: If the boolean is true, then use the one style ; if the boolean is false, use the other style. Here's the code in asp:

Code:
bColorPicker = TRUE

Do

If bColorPicker = TRUE Then

***** Write here the code to make the row in the first color*****

bColorPicker = FALSE

Else

***** Write here the code to make the row in the second color*****

bColorPicker = TRUE

End If

Loop Until MyRecordSet.EOF

Don't forget to re-assign the value of bColorPicker after each test. Otherwise the colors won't change.

I hope this helps you out,
Steven
 
You could do it with javascript if you wanted I suppose. Usually done when the server-side code generates the table though.
Code:
if rownum % 2 == 0
or something like that.
 
(Just curious, ConeHead, but is there a problem with the
Code:
rownum % 2
method?)
 
oh no... I was just thinking he could cut out the javascript and just use CF since that was what he was using... That was my point... I tend to just skim stuff and then comment... you know, speak before I think :) I don't think I even read yours... that looks good too! and shorter! Always a plus!

[conehead]
 
Ah, right. I didn't mean that he should use that with javascript btw; I meant he could do it that way server-side.
 
Thanks for everyone's help. I have solved my problem using the following logic.

<CFSET counter =0>
<CFLOOP query=&quot;qryTable&quot;>
<CFOUTPUT>
<CFSET counter=counter mod 2>
<CFIF counter IS 0>
<TR bgcolor=&quot;##fde99b&quot;>
<TD>
</TD>
<CFELSE>
<TR bgcolor=&quot;white&quot;>
<TD>
</TD>
</CFIF>
<CFSET counter=counter+1>
</CFOUTPUT>
</CFLOOP>
 
or you could do it the easy way next time
<table>
<cfoutput query = &quot;qryTable&quot;>
<tr bgcolor = &quot;<cfif qryTable.currentrow mod 2 eq 0>##fde99b<cfelse>##FFFFFF&quot;>
<td>
stuff
</td>
</tr>
</cfoutput>
</table>

thereptilian468x60.gif

 
This is what I use in CF:

<!--- IF statements alternate row colors --->
<cfif CurrentRow Mod 2>
<tr bgcolor=&quot;FFFFFF&quot;>
<cfelse>
<tr bgcolor=&quot;fde99b&quot;>
</cfif>

Same code as bombboy's, just a different layout.

Hope This Helps!

Ecobb
- I hate computers!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top