PDA

View Full Version : How to display grids in alternative colors?


lzhang
06-20-2006, 12:08 PM
I searched the forum, but not so successful. Thank you all.

_Aerospace_Eng_
06-20-2006, 12:09 PM
We need more info. What is the grid for? How big do you need it?

pj_anf
06-20-2006, 12:27 PM
i think they mean they want to display a table with alternating row colors:

<table>
<tr bgcolor="red"></tr>
<tr brcolor="blue"></tr>
</table>

that's my best guess?

lzhang
06-20-2006, 12:42 PM
I'm sorry. I meant one row displays blue, the other displays white as the background. so on and so forth, Thank you.

lzhang
06-21-2006, 09:52 AM
pj_anf, sorry. this is not what I want.

pj_anf
06-21-2006, 11:23 AM
<table>
<tr bgcolor="blue"></tr>
<tr brcolor="white"></tr>
<tr bgcolor="blue"></tr>
<tr brcolor="white"></tr>
</table>

????

coothead
06-21-2006, 11:48 AM
Hi there lzhang,

as you have posted this problem of yours in the 'Client Side Scripting' forum,
I presume that you must be looking for a dynamic solution. :agree:
If this is the case, then this is the solution...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>alternate table row background color</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">
<!--
#table1 {
border:1px solid #000;
margin:20px auto;
}
#table1 td {
width:100px;
height:30px;
border:1px solid #000;
text-align:center;
}
.blue {
background-color:#eef;
color:#000;
}
.white {
background-color:#fff;
color:#000;
}
-->
</style>

<script type="text/javascript">
<!--
window.onload=function() {
var rows=document.getElementsByTagName('tr');
for(c=0;c<rows.length;c++) {
if(c%2==0) {
rows[c].className='blue';
}
else {
rows[c].className='white';
}
}
}
//-->
</script>

</head>
<body>

<table id="table1">
<tr>
<td>cell</td><td>cell</td><td>cell</td>
</tr><tr>
<td>cell</td><td>cell</td><td>cell</td>
</tr><tr>
<td>cell</td><td>cell</td><td>cell</td>
</tr><tr>
<td>cell</td><td>cell</td><td>cell</td>
</tr><tr>
<td>cell</td><td>cell</td><td>cell</td>
</tr><tr>
<td>cell</td><td>cell</td><td>cell</td>
</tr>
</table>


</body>
</html>

lzhang
06-22-2006, 01:22 PM
Thank you for your reply. It seams that tr does not have "brcolor"

coothead
06-22-2006, 01:40 PM
Hi there lzhang,

brcolor is a typing error by pj_anf. :supereek:
bgcolor - the correct spelling - is a deprecated attribute and consequently should not have been suggested as a solution by pj_anf. :disagree:

lzhang
06-22-2006, 01:59 PM
Thank you very much, Coothead, I succeeded with your method!

karinne
06-22-2006, 02:00 PM
You can also do this in PHP if you'd like - http://www.sitepoint.com/forums/showthread.php?t=202485

lzhang
06-23-2006, 12:58 PM
Thank you, Karinne, I am not using php.

pj_anf
06-23-2006, 04:25 PM
errr sorry bout the typo's... i didn't even catch them till they were pointed out. I ment to write pseudo-code above what I posted as usual to keep in mind I didn't test it but just wrote it off the top of my head. Also in my defense, that initial code chunk was just to see if that was the idea they were trying to get.

coothead's right that the way i wrote that code out should really really be avoided though. The css method he described is a much better way to go about it. If your unfamilar with CSS formatting for this sort of thing there are plenty of great websites with tutorials on the basics of it. Or feel free to check out the CSS forum section of this website.