views:

35

answers:

4

Possible Duplicates:
How to alternate HTML table row colors using JSP?
Table row - Giving alternate colors

can any one provide basic code for dynamically adding rows in a table with alternate colors using css in jsp file kindly provide this code

A: 

Basically, there are two options:

  • either you have to add a style class to all even (or odd) rows in your jsp
  • or you can do it dynamically on the client with javascript (if that's an option)
seanizer
thanks seanizer
blood orchid
A: 

Take a look at the CSS odd and even selectors.

Lazarus
Do browsers support that? IE specifically?
seanizer
It's officially CSS 3 I believe so not before IE9, the alternative is to look at jQuery.com as that includes a css selector that does support these constructs and would allow you to create the alternating effect you are looking for.
Lazarus
answering my own question: No they don't. http://www.quirksmode.org/css/contents.html#t38
seanizer
@seanizer - True but hey, IE might not be a consideration for the OP.
Lazarus
If he's that lucky :-)
seanizer
Or just blasé ;)
Lazarus
A: 
<table>
<?
    boolean evenRow = true;
    for (int i = 0; i < numRowsToDisplay; i++)
    {
?>

<tr class="<?= evenRow? "evenrowstyle" : "oddrowstyle" ?>"><td>whatever</td></tr>

<?
        evenRow = !evenRow;
    }
?>
</table>
Riley
thanks riley for your support.
blood orchid
A: 

as in

<style>
#TableID tr:nt-child(odd){
background-color:white;
}

#TableID tr:nth-child(even){
background-color:silver;
}
</style>
FatherStorm
fails in several browsers: http://www.quirksmode.org/css/contents.html#t38
seanizer
thanks father storm
blood orchid