473,699 Members | 2,433 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2423
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.goo glegroups.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.equal s("red")){
theColor = "blue";
}else{
theColor = "red";
}

theString = "<tr style='backgrou nd-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.goo glegroups.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.equal s("red")){
theColor = "blue";
}else{
theColor = "red";
}

theString = "<tr style='backgrou nd-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.javas cript:
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.getEle mentById('myTab le');
var rws = myTable.getElem entsByTagName(" tr");
for(var i=0;i<rws.lengt h;i++)
rws[i].style.backgrou ndColor=(i%2==0 )?"red":"green" ;

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 23 '06 #5
sc*****@gmail.c om 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.javas cript:
sc*****@gmail.c om 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.getEle mentById('mytab le').tBodies[0].childNodes;
i = 0; while (i< x.length)
x(i).style.back groundColor = (i++ % 2) ? '#faa' : '#aaf';

or, using classes:

x = document.getEle mentById('mytab le').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
2507
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 question, is it possible to alternate the row colors automatic and have it created from a list of URL's?
4
3949
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 appropriate alt text, anyone viewing the page with images turned off, will see the alt text instead, and on hover will see the popups.
3
4756
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
2251
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 GIF and I have forgotten what the main page was. Apart from lining up the parts of the image, there was no discrepancy in colours between the two formats. Is that what "Web-safe" means? I thought that it just meant that the colours would be the...
7
5989
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 select="result"> <xsl:if test="@status = 'Pass'"> <tr><td>@name</td></tr> </xsl:if> <xsl:for-each>
4
2280
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 unable to alternate the colour of every row so that the table is lot more readable. can anyone suggest a javascript or even a css script which will alternate the row colours irrespective of the number of rows.
1
4346
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 specifying external style sheets in link using combinations of rel and using or not using the title attribute. I think I sort of understand rel="alternate stylesheet". It seems to be able to act something like the media="print" or media="handheld" or...
3
2195
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 control to have a different background colour on alternate rows. I'm using VS2005. Firstly, I'm surprised that I can't do it out of the box. But secondly, I can't find a really simple one-liner that let's me do it by overriding something.
5
4032
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 specific As Color = Color.FromKnownColor(known)
0
8687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8883
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4376
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3057
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2347
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2009
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.