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

Home Posts Topics Members FAQ

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 2280
VK

sc*****@gmail.c om 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.backgrou ndColor = (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>EXERCI SE 2</title>
<script language="javas cript">
function alternate(id)
{
if(document.get ElementsByTagNa me)
{
var table = document.getEle mentById(id);
var rows = table.getElemen tsByTagName("tr ");
for(i = 0; i < rows.length; i++)
{
if(i % 2 == 1)
rows[i].className = "even";
else
rows[i].className = "odd";
}
}
var tableid = document.getEle mentsByTagName( "table");
}
</script>
<style>
..odd
{
background-color: white;
}
..even {
background-color: pink;
}

</style>
</head>

<body onload="alterna te('TableID')">

<table border="1" id="TableID" class="sort-table">
<thead>
<tr>
<td><b>Column 1</b></td>
<td><b>Column 2</b></td>
<td><b>Column 3</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>alternative ly 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.javas cript:
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.getE lementById('tbl ').childNodes(0 ).childNodes
while(i< x.length) {
if (i % 2)
x(i).style.back groundColor="#e ee"
else
x(i).style.back groundColor="#a aa"
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>EXERCI SE 2</title>
<script language="javas cript">
The language attribute is deprecated, type is required:

<script type="text/javascript">



function alternate(id)
{
if(document.get ElementsByTagNa me)
{
var table = document.getEle mentById(id);
var rows = table.getElemen tsByTagName("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.getEle mentsByTagName( "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="alterna te('TableID')">

<table border="1" id="TableID" class="sort-table">
<thead>
<tr>
<td><b>Column 1</b></td>
<td><b>Column 2</b></td>
<td><b>Column 3</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
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?
5
2410
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 of each alternative? Does basic.css get downloaded each time the style is changed, or just once (with the first view)? Does it matter? - Is there another way? - -- tenxng@ban.arg.nh Email addy ROT13'd
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>
1
4637
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 (http://www.grisoft.com). Version: 6.0.512 / Virus Database: 309 - Release Date: 20/08/2003
3
3173
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
2423
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 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.
2
1655
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 as compared to enclosing each question and answer within divs ? Perhaps , through JavaScript. Thanks in advance ..
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.
18
4366
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 result of the query in a table. I would like some help with the code to colour the alternate rows please. function printQuery($param) { echo "<table width=100% valign=center>"; echo "<tr>"; foreach ($param as $key => $result)
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
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
9034
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7750
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6534
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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...
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.