shlagish
03-11-2005, 06:48 AM
After reading this article: http://www.alistapart.com/articles/zebratables/
I decided that I might want more than one of those tables in the same document, so I decided I should adapt the script to make it operate from class instead of id.
What this script does:
Makes the odd rows of a table have a different style than the even rows.
Doesn't override any class declaration put on the table, any tr or any td.
Requires only a class declaration on the table. If you want another class at the same time, do it: <table class='zebra bordered'>
Allows more than one table like this to be in the same document.
Anyways, here is my script:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Zebra Tables</title>
<style type="text/css">
<!--
.zebra { margin: 1em; }
.zebra tr td { font-family: verdana, sans-serif;
padding: 3px 8px;
border-left: 1px solid #d9d9d9;
}
.zebra tr.highlighted td { background: #3d80df;
color: #fff;
font-weight: bold;
}
.zebra tr td.unavailable { color: #f00;
text-decoration: line-through;
background: #eee;
}
.zebra tr td.odd { background: #edf3fe; }
.zebra tr td.even { background: #fff; }
.bordered { border: 5px solid #d9d9d9; }
/#For browser that support CSS3#/
.zebra tr:nth-child(odd) td { background: #edf3fe; }
.zebra tr:nth-child(even) td {background: #fff; }
/#End of browser-specific code#/
//-->
</style>
<script type="text/javascript">
<!--
/#This script is originally made by David F. Miller#/
/#I just brought a couple modifications#/
/#See the original script: http://www.alistapart.com/articles/zebratables/ #/
/# This function is needed to work around a bug in IE related to element attributes #/
function hasClass(obj) {
var result=false;
if(obj.getAttributeNode("class")!=null){
result=obj.getAttributeNode("class").value;
}
return result;
}
function zebra() {
var tables=document.getElementsByTagName('table'),
i;
for(i=0;i<tables.length;i++){
if(hasClass(tables[i]).match('zebra')){
var trs=tables[i].getElementsByTagName('tr'),
j,
even=0;
for(j=0;j<trs.length;j++){
if(!hasClass(trs[j])){
var tds=trs[j].getElementsByTagName('td'),
k;
for(k=0;k<tds.length;k++){
if(!hasClass(tds[k])){
tds[k].setAttribute('class',(even)?'even':'odd');
}
}
}
even=!even;
}
}
}
}
window.onload=zebra;
// -->
</script>
</head>
<body>
<h1>Zebra Tables</h1>
<table class="zebra" cellspacing="0">
<tr>
<td>1</td>
<td>Lost In The Plot</td>
<td>The Dears</td>
</tr>
<tr>
<td>2</td>
<td>Poison</td>
<td>The Constantines</td>
</tr>
<tr class="highlighted">
<td>3</td>
<td>Plea From A Cat Named Virtute</td>
<td>The Weakerthans</td>
</tr>
<tr>
<td>4</td>
<td>Melissa Louise</td>
<td>Chixdiggit!</td>
</tr>
<tr>
<td>5</td>
<td>Living Room</td>
<td>Tegan And Sara</td>
</tr>
<tr>
<td>6</td>
<td>Speed</td>
<td>Bran Van 3000</td>
</tr>
<tr>
<td>7</td>
<td>Fast Money Blessing</td>
<td>King Cobb Steelie</td>
</tr>
<tr>
<td>8</td>
<td class="unavailable">Sore</td>
<td>Buck 65</td>
</tr>
<tr>
<td>9</td>
<td>Love Travel</td>
<td>Danko Jones</td>
</tr>
<tr>
<td>10</td>
<td>You Never Let Me Down</td>
<td>Furnaceface</td>
</tr>
</table>
<table class="zebra bordered" cellspacing="0">
<tr>
<td>1</td>
<td>Lost In The Plot</td>
<td>The Dears</td>
</tr>
<tr>
<td>2</td>
<td>Poison</td>
<td>The Constantines</td>
</tr>
<tr>
<td>3</td>
<td>Plea From A Cat Named Virtute</td>
<td>The Weakerthans</td>
</tr>
<tr class="highlighted">
<td>4</td>
<td>Melissa Louise</td>
<td>Chixdiggit!</td>
</tr>
<tr>
<td>5</td>
<td>Living Room</td>
<td>Tegan And Sara</td>
</tr>
<tr>
<td>6</td>
<td>Speed</td>
<td>Bran Van 3000</td>
</tr>
<tr>
<td>7</td>
<td>Fast Money Blessing</td>
<td>King Cobb Steelie</td>
</tr>
<tr>
<td>8</td>
<td>Sore</td>
<td>Buck 65</td>
</tr>
<tr>
<td>9</td>
<td class="unavailable">Love Travel</td>
<td>Danko Jones</td>
</tr>
<tr>
<td>10</td>
<td>You Never Let Me Down</td>
<td>Furnaceface</td>
</tr>
</table>
</body>
</html>
What do you think?
p.s the content of the tables, the colours and the hasClass() function are not my creation, remember, it's just an adaption to an existing script.
I decided that I might want more than one of those tables in the same document, so I decided I should adapt the script to make it operate from class instead of id.
What this script does:
Makes the odd rows of a table have a different style than the even rows.
Doesn't override any class declaration put on the table, any tr or any td.
Requires only a class declaration on the table. If you want another class at the same time, do it: <table class='zebra bordered'>
Allows more than one table like this to be in the same document.
Anyways, here is my script:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Zebra Tables</title>
<style type="text/css">
<!--
.zebra { margin: 1em; }
.zebra tr td { font-family: verdana, sans-serif;
padding: 3px 8px;
border-left: 1px solid #d9d9d9;
}
.zebra tr.highlighted td { background: #3d80df;
color: #fff;
font-weight: bold;
}
.zebra tr td.unavailable { color: #f00;
text-decoration: line-through;
background: #eee;
}
.zebra tr td.odd { background: #edf3fe; }
.zebra tr td.even { background: #fff; }
.bordered { border: 5px solid #d9d9d9; }
/#For browser that support CSS3#/
.zebra tr:nth-child(odd) td { background: #edf3fe; }
.zebra tr:nth-child(even) td {background: #fff; }
/#End of browser-specific code#/
//-->
</style>
<script type="text/javascript">
<!--
/#This script is originally made by David F. Miller#/
/#I just brought a couple modifications#/
/#See the original script: http://www.alistapart.com/articles/zebratables/ #/
/# This function is needed to work around a bug in IE related to element attributes #/
function hasClass(obj) {
var result=false;
if(obj.getAttributeNode("class")!=null){
result=obj.getAttributeNode("class").value;
}
return result;
}
function zebra() {
var tables=document.getElementsByTagName('table'),
i;
for(i=0;i<tables.length;i++){
if(hasClass(tables[i]).match('zebra')){
var trs=tables[i].getElementsByTagName('tr'),
j,
even=0;
for(j=0;j<trs.length;j++){
if(!hasClass(trs[j])){
var tds=trs[j].getElementsByTagName('td'),
k;
for(k=0;k<tds.length;k++){
if(!hasClass(tds[k])){
tds[k].setAttribute('class',(even)?'even':'odd');
}
}
}
even=!even;
}
}
}
}
window.onload=zebra;
// -->
</script>
</head>
<body>
<h1>Zebra Tables</h1>
<table class="zebra" cellspacing="0">
<tr>
<td>1</td>
<td>Lost In The Plot</td>
<td>The Dears</td>
</tr>
<tr>
<td>2</td>
<td>Poison</td>
<td>The Constantines</td>
</tr>
<tr class="highlighted">
<td>3</td>
<td>Plea From A Cat Named Virtute</td>
<td>The Weakerthans</td>
</tr>
<tr>
<td>4</td>
<td>Melissa Louise</td>
<td>Chixdiggit!</td>
</tr>
<tr>
<td>5</td>
<td>Living Room</td>
<td>Tegan And Sara</td>
</tr>
<tr>
<td>6</td>
<td>Speed</td>
<td>Bran Van 3000</td>
</tr>
<tr>
<td>7</td>
<td>Fast Money Blessing</td>
<td>King Cobb Steelie</td>
</tr>
<tr>
<td>8</td>
<td class="unavailable">Sore</td>
<td>Buck 65</td>
</tr>
<tr>
<td>9</td>
<td>Love Travel</td>
<td>Danko Jones</td>
</tr>
<tr>
<td>10</td>
<td>You Never Let Me Down</td>
<td>Furnaceface</td>
</tr>
</table>
<table class="zebra bordered" cellspacing="0">
<tr>
<td>1</td>
<td>Lost In The Plot</td>
<td>The Dears</td>
</tr>
<tr>
<td>2</td>
<td>Poison</td>
<td>The Constantines</td>
</tr>
<tr>
<td>3</td>
<td>Plea From A Cat Named Virtute</td>
<td>The Weakerthans</td>
</tr>
<tr class="highlighted">
<td>4</td>
<td>Melissa Louise</td>
<td>Chixdiggit!</td>
</tr>
<tr>
<td>5</td>
<td>Living Room</td>
<td>Tegan And Sara</td>
</tr>
<tr>
<td>6</td>
<td>Speed</td>
<td>Bran Van 3000</td>
</tr>
<tr>
<td>7</td>
<td>Fast Money Blessing</td>
<td>King Cobb Steelie</td>
</tr>
<tr>
<td>8</td>
<td>Sore</td>
<td>Buck 65</td>
</tr>
<tr>
<td>9</td>
<td class="unavailable">Love Travel</td>
<td>Danko Jones</td>
</tr>
<tr>
<td>10</td>
<td>You Never Let Me Down</td>
<td>Furnaceface</td>
</tr>
</table>
</body>
</html>
What do you think?
p.s the content of the tables, the colours and the hasClass() function are not my creation, remember, it's just an adaption to an existing script.