Alternate row colours

S

sconeek

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.
 
L

Lee

(e-mail address removed) said:
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.

This has nothing to do with Javascript.
Is your problem that you don't know how to alternate a variable
between two values on each pass through the loop, or that you
don't know how to set the background color of an HTML table row?
 
S

sconeek

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.
 
L

Lee

(e-mail address removed) said:
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.

Please use your SHIFT key.

Why don't you simply change the background color on each pass
through the foreach code? The correct way to do this is a
Java question. This newsgroup has nothing to do with Java.
 
H

Hal Rosser

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
 
Z

Zif

Hal said:
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.
 
E

Evertjan.

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.style.backgroundColor=(i%2==0)?"red":"green";
 
T

Thomas 'PointedEars' Lahn

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
 
E

Evertjan.

Thomas 'PointedEars' Lahn wrote on 16 feb 2006 in comp.lang.javascript:
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';
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top