This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes Changing the colors of rows in a table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Changing the colors of rows in a table" Watch "Changing the colors of rows in a table" New topic
Author

Changing the colors of rows in a table

Tony Walters
Ranch Hand

Joined: Feb 13, 2003
Posts: 54
Hi all
Tried posting this in the JSP forum with no joy (though several suggestions), and was advised to try here.
I am using <logic:iterate> to iterate over rows in a table. I want the rows to be in alternating colours to make them more distinguishable, and figured i could write a tag to do it. The tag must contain no html.
Any suggestions?
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
Upon further reflection, here's an idea, although it requires multiple tags...
1) Declare a loop counter for your iterate tag,
<logic:iterate indexId="iterCount" ... >
2) Create a tag that defines a bean (similar to the bean define tag) based on the loop counter, i.e. <app:styleDefine id="rowStyle" name="iterCount" mod0Style="evenStyle" mod1Style="oddStyle" />
This tag simply takes the value of iterCount, mods it by 2, and uses it to choose either mod0Style or mod1Style. This value is then defined in id (rowStyle).
3) On your TR tag, you can set the style with the new bean.
<TR style="<bean:write name='rowStyle' />" > <!-- row content --> </TR>
This is far more kludge than a pure row iteration allowed to generate HTML, but it should work...
[ February 18, 2003: Message edited by: David Hibbs ]

"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
The way I solved this, without scriptlets, is to write a custom tag that would produce alternating color <tr> tags every time it was called. It basically just kept a static variable, which could be reset (in order to keep your alternations consistent), and had parameters for bgcolor1 and bgcolor2. It would look something like this...
<!-- Initialize the tag -->
<app:altColorTr init="true"/>
<logic:iterate id="i" name="myBean" property="myProperty">
<app:altColorTr bgcolor1="#e0e0e0" bgcolor2="#ffffff">
<td><bean:write name="i"/></td>
</app:altColorTr>
</logic:iterate>
So the first iteration would produce at grey row, the next a white row, and then it would repeat. Of course instead of keeping a static counter it would probably be smarter to pass in an index. But you get the idea.
[ February 17, 2003: Message edited by: Jason Menard ]
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
Originally posted by Jason Menard:
The way I solved this, without scriptlets, is to write a custom tag that would produce alternating color <tr> tags every time it was called.

Yes, I suggested this in the other thread he mentioned. However, this is contrary to his requirement of not generating any HTML. (Which I still maintain is a bogus requirement. But we've all dealt with those from time to time, right? )
It dawned on me that you can combine my steps 2 and 3 above into a single tag taking 3 parms, which is a little less of a kludge than my original concept. However, the original concept is still useful in order to understand my suggestion.
On your TR tag, you can set the style with the new tag. Something like this...
<TR style="<app:rowStyle name='iterCount' mod0Style='evenStyle' mod1Style='oddStyle' />" > <!-- row content --> </TR>
Matthew Marquand
Ranch Hand

Joined: Dec 19, 2002
Posts: 32
Question: Wouldn't the static variable get womped on by two pages being rendered at the same time?
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
Originally posted by Matthew Marquand:
Question: Wouldn't the static variable get womped on by two pages being rendered at the same time?

What static variable?
Matthew Marquand
Ranch Hand

Joined: Dec 19, 2002
Posts: 32
I was referring to Jason Menard's post of using a static class variable in the tag implementation to keep track of usage as the logic in the JSP/Servlet is run. Since the tag class is shared across all instances using it in the same JVM, then the setting/initialization would be "noticed" by other pages using the same tag.
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Absolutely! That's why I mentioned it would be better to pass in an index variable.
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
Originally posted by Jason Menard:
Absolutely! That's why I mentioned it would be better to pass in an index variable.

Aha, I see the 'static' reference in question.
Passing in an index variable would still be inherently unsafe (thread wise) if you made it static. Declaring it static means that it's shared among all instances of your tag.
Consider this scenario.
at time=1ms, Thread 1 successfully calls the setter with value "3".
at time=3ms, Thread 2 successfully calls the setter with value "8".
at time=4ms, Thread 1 enters doStartTag and progresses through doEndTag.
If the attribute is static, your row is now the wrong color!
What I think was meant is simply an instance variable. In this case, you have no threading issues.
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Right, the index wouldn't be static, but an instance variable. It looks like you could use the indexId attribute of <logic:iterate>, although I've never tried it.
[ February 20, 2003: Message edited by: Jason Menard ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Changing the colors of rows in a table
 
Similar Threads
Need help with TOMAHAWK Calendar t:inputCalendar
JSF and Layouts
logic iterate
doubt in html
Advanced editable TableTag in JSP