By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,651 Members | 1,908 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,651 IT Pros & Developers. It's quick & easy.

Alternate row colours

P: n/a
hi all,
my java servlet code has a foreach loop which automatically generates a
HTML table with x number of rows.
now my problem is that I would like the rows to be alternating in
colour so that its a lot more easier to read. i have tried to implement
a few approaches but everytime it fails.
can somebody help me out with us and suggest a solution.
thanks.

Jan 23 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
i have a foreach loop in my HTML code which takes a java variable and
for each instance of that variable generates a tr within my table. now
if i hardcode the background for the tr it happens to every single row.
i want to alternate the row colours so that the table rows are a lot
more easier to read. hope this clarifies my question.

Jan 23 '06 #2

P: n/a

<sc*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
i have a foreach loop in my HTML code which takes a java variable and
for each instance of that variable generates a tr within my table. now
if i hardcode the background for the tr it happens to every single row.
i want to alternate the row colours so that the table rows are a lot
more easier to read. hope this clarifies my question.

Almost anything can be fixed with an if statement.
Use a string variable to hold the color.
And before you use it in the tr tag, have an if statement something like
this

if (theColor.equals("red")){
theColor = "blue";
}else{
theColor = "red";
}

theString = "<tr style='background-color:" + theColor + "'>";
----or something along those lines ----
BTW this is a javascript group - and javascript is not java
Jan 23 '06 #3

P: n/a
Zif
Hal Rosser wrote:
<sc*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
i have a foreach loop in my HTML code which takes a java variable and
for each instance of that variable generates a tr within my table. now
if i hardcode the background for the tr it happens to every single row.
i want to alternate the row colours so that the table rows are a lot
more easier to read. hope this clarifies my question.


Almost anything can be fixed with an if statement.
Use a string variable to hold the color.
And before you use it in the tr tag, have an if statement something like
this

if (theColor.equals("red")){
theColor = "blue";
}else{
theColor = "red";
}

theString = "<tr style='background-color:" + theColor + "'>";
----or something along those lines ----


That logic is not going to achieve what the OP wants. It will change
all red rows to blue, and everything else to red.

The usual way to do it is to either use the row index or keep a count of
the rows. Then evaluate the index/counter mod 2 - if it's zero, set it
to the 'even' colour; otherwise set it to the 'odd' colour.
--
Zif
Jan 23 '06 #4

P: n/a
wrote on 23 jan 2006 in comp.lang.javascript:
hi all,
my java servlet code has a foreach loop which automatically generates a
HTML table with x number of rows.
now my problem is that I would like the rows to be alternating in
colour so that its a lot more easier to read. i have tried to implement
a few approaches but everytime it fails.
can somebody help me out with us and suggest a solution.
thanks.


var myTable = document.getElementById('myTable');
var rws = myTable.getElementsByTagName("tr");
for(var i=0;i<rws.length;i++)
rws[i].style.backgroundColor=(i%2==0)?"red":"green";

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 23 '06 #5

P: n/a
sc*****@gmail.com wrote:
my java servlet code has a foreach loop which automatically generates a
HTML table with x number of rows. now my problem is that I would like the
rows to be alternating in colour so that its a lot more easier to read.


While it would be possible easily with CSS3 Selectors (Last Call Working
Draft) --

tr:nth-child(odd) {
background-color:red;
color:black;
}

tr:nth-child(even) {
background-color:blue;
color:white;
}

-- you should ask in a newsgroup dealing with _Java_ to have those rows
formatted as generated by your servlet (probably involving CSS classes
and the above CSS color properties). Surely you also want those rows
in alternating color when client-side script support is not available.
PointedEars
Feb 16 '06 #6

P: n/a
Thomas 'PointedEars' Lahn wrote on 16 feb 2006 in comp.lang.javascript:
sc*****@gmail.com wrote:
my java servlet code has a foreach loop which automatically generates
a HTML table with x number of rows. now my problem is that I would
like the rows to be alternating in colour so that its a lot more
easier to read.
[....] Surely you also want
those rows in alternating color when client-side script support is not
available.


This being a javascript NG however, try:

x = document.getElementById('mytable').tBodies[0].childNodes;
i = 0; while (i< x.length)
x(i).style.backgroundColor = (i++ % 2) ? '#faa' : '#aaf';

or, using classes:

x = document.getElementById('mytable').tBodies[0].childNodes;
i = 0; while (i< x.length)
x(i).className = (i++ % 2) ? 'redRow' : 'blueRow';

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 16 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.