473,327 Members | 1,930 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,327 software developers and data experts.

Alternate row colour

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.

thanks.

Dec 9 '05 #1
4 2252
VK

sc*****@gmail.com wrote:
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.


Difficult to say exactly w/o knowing how your data is coming to the
browser: as a pre-generated HTML code or some raw data you're using to
populate your table?

Something like (pseudo-code):

var oddRowColor = '#FFFFFF';
var evenRowColor="'#C0C0C0';
[loop]
objRow[i].style.backgroundColor = (i%2) ?
oddRowColor : evenRowColor;
[/loop]

Dec 9 '05 #2
The follwing code will automatically draw colours to the table. The css
is defined and the body onload function calls the alternate function
which gives alternate colours to the table...
Hope this will be useful to you!

<html>

<head>
<title>EXERCISE 2</title>
<script language="javascript">
function alternate(id)
{
if(document.getElementsByTagName)
{
var table = document.getElementById(id);
var rows = table.getElementsByTagName("tr");
for(i = 0; i < rows.length; i++)
{
if(i % 2 == 1)
rows[i].className = "even";
else
rows[i].className = "odd";
}
}
var tableid = document.getElementsByTagName("table");
}
</script>
<style>
..odd
{
background-color: white;
}
..even {
background-color: pink;
}

</style>
</head>

<body onload="alternate('TableID')">

<table border="1" id="TableID" class="sort-table">
<thead>
<tr>
<td><b>Column1</b></td>
<td><b>Column2</b></td>
<td><b>Column3</b></td>
</tr>
</thead>
<tr>
<td>1</td>
<td>one</td>
<td>This is a test code</td>
</tr>
<tr>
<td>2</td>
<td>two</td>
<td>This will work</td>
</tr>
<tr>
<td>3</td>
<td>three</td>
<td>This will automaically</td>
</tr>
<tr>
<td>4</td>
<td>four</td>
<td>give color to the table</td>
</tr>
<tr>
<td>5</td>
<td>five</td>
<td>alternatively using a css</td>
</tr>
<tr>
<td>6</td>
<td>six</td>
<td>using the body onload </td>
</tr>
<tr>
<td>7</td>
<td>seven</td>
<td>function</td>
</tr>
</table>

</body>

</html>

Dec 9 '05 #3
wrote on 09 dec 2005 in comp.lang.javascript:
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.


I suggested this in 2003 on usenet:
[use childNodes(1) if you have a <thead> before the <tbody>,
even if the <tbody> is not explicitly declared]
<script type='text/javascript'>
i=0
x=document.getElementById('tbl').childNodes(0).chi ldNodes
while(i< x.length) {
if (i % 2)
x(i).style.backgroundColor="#eee"
else
x(i).style.backgroundColor="#aaa"
i++
}
</script>
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Dec 9 '05 #4
K S Jayakumar wrote:
The follwing code will automatically draw colours to the table. The css
is defined and the body onload function calls the alternate function
which gives alternate colours to the table...
Hope this will be useful to you!

<html>

<head>
<title>EXERCISE 2</title>
<script language="javascript">
The language attribute is deprecated, type is required:

<script type="text/javascript">



function alternate(id)
{
if(document.getElementsByTagName)
{
var table = document.getElementById(id);
var rows = table.getElementsByTagName("tr");
for(i = 0; i < rows.length; i++)
{
if(i % 2 == 1)
rows[i].className = "even";
That will give the odd rows a class of 'even' (not that it really
matters but it might be confusing). A simpler test is:

if( i%2 )
rows[i].className = "even";

else
rows[i].className = "odd";
You can also get rid of the else statement - give all rows a set colour
then just change the the even (or odd) ones. Since you have set all the
odd ones to white:

for(var i=0, len=rows.length; i<len; i+=2) {
rows[i].className = "even";
}

}
}
var tableid = document.getElementsByTagName("table");
Left over from debug?

}
</script>
<style>
The type attribute is required for style elements too:

<style type="text/css">

.odd
{
background-color: white;
}
.even {
background-color: pink;
}

</style>
</head>

<body onload="alternate('TableID')">

<table border="1" id="TableID" class="sort-table">
<thead>
<tr>
<td><b>Column1</b></td>
<td><b>Column2</b></td>
<td><b>Column3</b></td>
</tr>
</thead>
If you don't want the header rows included in the row count, put in a
tbody element and use that as a reference instead of the table:

<tbody id="...whatever...">
<tr> [...]
</tr>
</tbody>
</table>

</body>

</html>

--
Rob
Dec 9 '05 #5

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...
5
by: User | last post by:
If I want to provide alternate stylesheets (eg red.css, green.css), is it better to put all the stuff that is common to both sheets in a separate css file (eg basic.css) and use @import at the top...
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...
1
by: news | last post by:
Hi all, How do you implement alternate line colors in ms access datasheet view Regards Joe --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system...
3
by: buck | last post by:
reports/ can we alternate formatting such as "no shading" and then "shading" for multiple records such as a phone directory in Access2000 reports
6
by: sconeek | last post by:
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...
2
by: viveklinux | last post by:
Hi, Have a FAQ page , where have many sets of questions and answers. The questions need to be a different colour / style and the answers different. Was wondering is there a easy way to do this...
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...
18
by: JohnDriver | last post by:
Hi, I am happy to say that with your help, I have been performing good in Ajax. Thanks for helping me to start with. I have a small problem now. I am pulling records from database and passing...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
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...
1
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.