An Improved Approach to Building Zebra Tables - Setting up a final hands-on example
(Page 4 of 4 )
In the prior section, I showed you how to improve the signature of the pertinent “buildZebraTable()” JavaScript function to provide it with the ability to build zebra tables that include multiple <tbody> sections. However, the function itself can look rather unintelligible if it’s not linked with a target (X)HTML table, as well as the corresponding CSS styles.
Thus, below I included the complete source code of a brand new (X)HTML file, which demonstrates how to use the recently-modified function to build a basic zebra table.
That being said, here’s how this sample file looks:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Example on building a simple zebra table with CSS and JavaScript (improved version)</title>
<style type="text/css">
body{
padding: 0;
margin: 0;
background: #fff;
}
h1{
font: bold 16pt Arial, Helvetica, sans-serif;
color: #000;
}
p{
font: normal 10pt Arial, Helvetica, sans-serif;
color: #000;
margin: 0;
}
#zebratable{
width: 40%;
text-align: center;
}
.oddrow{
background: #eee;
}
.evenrow{
background: #ccc;
}
</style>
<script language="javascript">
// define 'buildZebraTable()' function
function buildZebraTable(tableId){
var table=document.getElementById(tableId);
if(!table){return};
// get all <tbody> table elements
var tbodies=document.getElementsByTagName('tbody');
for(var i=0;i<tbodies.length;i++){
var evenFlag=false;
// get all <tr> table elements
var trows=document.getElementsByTagName('tr');
for(var j=0;j<trows.length;j++){
// assign CSS class to even and odd rows
trows[j].className=!evenFlag?'oddrow':'evenrow';
evenFlag=!evenFlag;
}
}
}
// run 'buildZebraTable()' function when web page is loaded
window.onload=function(){
if(document.getElementById&&document.
getElementsByTagName&&document.createElement){
buildZebraTable('zebratable');
}
}
</script>
</head>
<body>
<h1>Example on building a simple zebra table with CSS and JavaScript (improved version)</h1>
<table id="zebratable">
<tbody>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
</tbody>
<tbody>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
</tbody>
<tbody>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
</tbody>
</table>
</body>
</html>
As you can see, the above (x)HTML file includes a regular table that contains three different <tbody> sections. Nevertheless, the “buildZebraTable()” JavaScript function perfectly adapts itself to these brand new circumstances. Once the pertinent web document has been loaded, it iterates over the table in question and automatically turns it into a zebra element!
Finally, to complement the previous practical example, I included a screen shot that shows the visual appearance of the zebra table just constructed. Here it is:

Now that you've hopefully grasped the logic that drives the construction of zebra tables with JavaScript and CSS, I suggest you use all of the code samples developed in this tutorial and build your own test examples.
Final thoughts
In this third installment of the series, I showed you how to create a simple JavaScript function aimed at building zebra tables that include multiple <tbody> sections within their corresponding markup. As you saw for yourself, the whole process is very comprehensive.
Nonetheless, there’s one topic that remains uncovered with reference to building zebra tables. As you’ve surely noticed, the JavaScript function defined previously uses two different CSS classes to alternately style the even and odd rows of the selected table. However, it’s also possible to utilize the “style” (yet non-standard) object to directly manipulate the background color of each row.
This alternative technique will be covered in depth in the last article of the series, so you don’t have any excuses to miss it!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |