File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes HTML, CSS and JavaScript and the fly likes How  to print rows in different colors in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How  to print rows in different colors in jsp" Watch "How  to print rows in different colors in jsp" New topic

How to print rows in different colors in jsp

giri shetty

Joined: Jan 22, 2004
Posts: 27
Hi guys,
i have a question ,i have a jsp using STRUTS an i have to print details in
in row wise and i have to maintain a different color for each consicutive row .

Alan Ford
Ranch Hand

Joined: Oct 26, 2003
Posts: 107
what i did is: my backing page is extended with object for support of switching those colors. so it will return diferent string (the one(s) defined in css for both colors). So in my for loop I have tags that return different names (every second time till end of loop).

TNT<br />MCP, SCJP 1.4,
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

Moving to the HTML/Javascript/CSS forum.

[Asking smart questions] [About Bear] [Books by Bear]
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
you can change the style of the row with

<tr style="background:#C0C0C0">

If you are wrting out the rows with the server side code, then do a mod(%) check with the current row count and alternate the colors.

There is a way to do it with JavaScript, but it is better to do it on the server side.

Julia Reynolds
Ranch Hand

Joined: May 31, 2001
Posts: 123
Here is some row alternating code from Ted Husted's Struts FAQ :
<% int count=0; %>
<struts:iterate id="row" name="currentReport" property="data" >
<tr BGCOLOR='<%= (count++ % 2==0 ?"#FFFFAA":"#EEEEEE") %>' >
<td> <%= count %> </td>
<struts:iterate id="item" collection="<%= row %>" >
<td nowrap align=MIDDLE > <%=(item).toString() %> </td>
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Folks should really embrace the 21st century and use CSS. HTML 3.2 is dead (or at least I wish it was!) I use class="odd" and class="even" for my alternating table rows and then include something like the following in the CSS file:

A pretty yucky effect with those colours but, hey!
If you crowbar that into Julia's scary Struts example you can really - um - strut your stuff! :roll:

I agree. Here's the link:
subject: How to print rows in different colors in jsp
It's not a secret anymore!