JavaScript
  Home arrow JavaScript arrow Page 2 - An Improved Approach to Building Zebra Tab...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

An Improved Approach to Building Zebra Tables
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-06-25

    Table of Contents:
  • An Improved Approach to Building Zebra Tables
  • Constructing zebra tables dynamically
  • Working with tables that contain multiple tbody sections
  • Setting up a final hands-on example

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    An Improved Approach to Building Zebra Tables - Constructing zebra tables dynamically


    (Page 2 of 4 )

    Zebra tables can be automatically styled by way of a simple JavaScript function that should perform two well-differentiated tasks: first, it iterates over the markup of the selected table, and second, it alternately styles its respective even and odd rows in order to assign to each of them different background colors.

    In the prior article of the series I demonstrated how to define a function, called “buildZebraTable(),” like the one described. It is implemented in the following way:


    <!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</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">

    function buildZebraTable(tableId){

    var table=document.getElementById(tableId);

    if(!table){return};

    var evenFlag=false;

    var rows=document.getElementsByTagName('tr');

    for(var i=0;i<rows.length;i++){

    rows[i].className=!evenFlag?'oddrow':'evenrow';

    evenFlag=!evenFlag;

    }

    }

    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</h1>

    <table id="zebratable">

    <tbody>

    <tr>

    <td><p>Content for cells of odd row goes here</p></td>

    </tr>

    <tr>

    <td><p>Content for cells of even row goes here</p></td>

    </tr>

    <tr>

    <td><p>Content for cells of odd row goes here</p></td>

    </tr>

    <tr>

    <td><p>Content for cells of even row goes here</p></td>

    </tr>

    <tr>

    <td><p>Content for cells of odd row goes here</p></td>

    </tr>

    <tr>

    <td><p>Content for cells of even row goes here</p></td>

    </tr>

    <tr>

    <td><p>Content for cells of odd row goes here</p></td>

    </tr>

    <tr>

    <td><p>Content for cells of even row goes here</p></td>

    </tr>

    <tr>

    <td><p>Content for cells of odd row goes here</p></td>

    </tr>

    <tr>

    <td><p>Content for cells of even row goes here</p></td>

    </tr>

    </tbody>

    </table>

    </body>

    </html>


    After carefully examining the above hands-on example, you’ll realize that building a basic zebra table with JavaScript is actually a straightforward process that can be performed with minor hassles. For this specific case, the “buildZebraTable()” function accesses the target table by its ID attribute, iterates over its even and odd rows, and finally assigns the appropriate CSS classes to them in order to achieve the so-called zebra visual appearance. Pretty simple to grasp, right?

    All right, at this moment you should feel pretty satisfied, since you learned how to automatically build zebra tables by way of a simple JavaScript function, without needing to style their rows from scratch. What else can you ask for?

    Well, despite the fact that this glorious achievement does deserve throwing a party, there’s an important issue that must be addressed concerning the creation of zebra tables with a basic JavaScript function. As you may have noticed, the sample (X)HTML table that I just showed you contains only one <tbody> section, but a regular table may include many of them.

    Thus, it’s necessary to slightly modify the signature of the “buildZebraTable()” JavaScript function so it can be used to work with tables that contains multiple <tbody> sections.

    This modification will be performed in the course of the section to come, so I recommend you click on the link that appears below and keep reading.

    More JavaScript Articles
    More By Alejandro Gervasio


       · This third tutorial of the series will show you how to improve the signature of the...
       · Nice article.If the page contains two tables it is applying for both table...
       · Thank you for the kind comments on my article. With reference to your question, it’s...
     

    JAVASCRIPT ARTICLES

    - Comparing Fields and Customizing Error Messa...
    - Checking Numbers and File Extensions with jQ...
    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek