When generating html tabular data, I just wanted to hear what methods you use to alternate the table row color? For my purposes, I am using Java and JSPs on the backend. I was just planning on looping through the data and if index % 2 == 0 set it to one color else set it to something else. Is this ok? Maybe it would be better if I used some sort of tag library?
If you can use JSTL (much similar to Java-like solution, but better),
<c:forEach var="myItem" items="${myCollection}" varStatus="loop">
<tr class="${loop.index % 2 == 0 ? 'even' : 'odd'}">
...
</tr>
</c:forEach>
Now have the CSS classes to define colors or other styles.
You can also consider using 'display' tag which does the same thing in server side or jQuery on client side to select rows with odd, even selectors and add the classes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With