473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Alternate row colours

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
6 2407
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

<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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: aznFETISH | last post by:
I have a list of links that I ue on my page, I alternate background colors for these links in a table, I usually do it using a DB but this list of link is manually added into the page so my...
4
by: Els | last post by:
Hi, I would like an opinion on the following: I have a page which is made up of background-images with transparent linked images in front of it, which on hover show text in CSS popups. Due to...
3
by: Peter Williams | last post by:
Hi All, I want to write some javascript for a html page which does the following. Imagine that the page contains a table with 2 columns and 3 rows, e.g.: +---+---+ | A | B | +---+---+
8
by: Doug Laidlaw | last post by:
I tried to grab an image from a Web page the other day. It turned out that the page was made up of three horizontal bands, and part of the image was in each. One band was a JPEG, another was a...
7
by: Matt B | last post by:
I have a need to alternate output row colour where not every row in the sequence is output, so I cannot base the colour on whether position() is odd or even. e.g. .... <xsl:for-each...
4
by: sconeek | last post by:
Hi all, I am generating a html based table with multiple rows of data coming in real time from a postgres DB. The underlying technology is java based however the front end is html. now i am...
1
by: Eric Lindsay | last post by:
I am trying to understand the differences between and uses of persistent, default and alternate styles. I have read http://www.w3.org/TR/REC-html40/present/styles.html section 14.3.2 on...
3
by: Charles Law | last post by:
Ok. I haven't been around for a while, so I hope someone will come to my rescue. I thought I had done this years ago, but I have scoured my code snippets and can't find it. I want my ListView...
5
by: Jameson | last post by:
Hi, I have a list of known colours, generated using: Dim colorNames As New System.Collections.Generic.List(Of String) For Each known As KnownColor In .GetValues(GetType(KnownColor)) Dim...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.