Home arrow JavaScript arrow Page 4 - An Improved Approach to Building Zebra Tables
JAVASCRIPT

An Improved Approach to Building Zebra Tables


Welcome to the educational series that shows how to build zebra tables with CSS and JavaScript. In this third tutorial of the series I’m going to show you how to improve the signature of the JavaScript function defined in the last article. This will make it suitable for working with tables that include different <tbody> sections.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
June 25, 2008
TABLE OF CONTENTS:
  1. · An Improved Approach to Building Zebra Tables
  2. · Constructing zebra tables dynamically
  3. · Working with tables that contain multiple tbody sections
  4. · Setting up a final hands-on example

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
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.

blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials