This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes JSP and the fly likes A different value on odd an even number in iteration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "A different value on odd an even number in iteration" Watch "A different value on odd an even number in iteration" New topic
Author

A different value on odd an even number in iteration

Sverre Moe
Ranch Hand

Joined: Jul 10, 2007
Posts: 110
I am iterating over a table, producing a new row at each iteration. What I need is to use a different class="" for the even and odd numbers.

The first row will start with a light row, while the next will have a dark row. Light row will occour in row/iteration 1,3,5,7,9,11,13, etc. and dark will occour in 2,4,6,8,10, etc.

How can I perform this without doing some mathematical operations in the iteration number?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You could do it by toggling a flag with each iteration but that would be a lot more work than using the modulus operator --which is a one liner.



Why don't you want do to do this with math?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Sverre Moe
Ranch Hand

Joined: Jul 10, 2007
Posts: 110
Originally posted by Ben Souther:
You could do it by toggling a flag with each iteration but that would be a lot more work than using the modulus operator --which is a one liner.



Why don't you want do to do this with math?


I thought it would be more code, but that was very elegant and better than my 5 line c:choose, c:when code.

I was unaware that I could have boolean logic within a html tag like that without c:if, c:choose/c:when.
[ February 19, 2008: Message edited by: Sverre Moe ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Sverre Moe:

I was unaware that I could have boolean logic within a html tag like that without c:if, c:choose/c:when.


Just so it's clear, that logic isn't being done by the HTML interpreter (which is on the client's machine). The ${} tag is a JSP Expression Language (EL) expression and is performed on the server. If you look at the generated HTML source, you'll only see the resulting class names, not the expression itself.

There is a copy of the JSP spec in my signature.
It shouldn't take more than 20 minutes to skim the whole thing.
This will give you a good idea of what JSP 2.0 has to offer.
[ February 19, 2008: Message edited by: Ben Souther ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A different value on odd an even number in iteration
 
Similar Threads
alternate rows in one color
Turn lights on and off with Java
IsometricTile
Specifying row classes in dataTables
Retrieving List from DB and setting to JSP in struts 2