JavaScript
  Home arrow JavaScript arrow Page 3 - 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 - Working with tables that contain multiple tbody sections


    (Page 3 of 4 )

    In accordance with the concepts that I deployed in the section that you just read, the definition of the previous “buildZebraTable()” JavaScript needs some tweaks, since it must be provided with the ability to construct zebra tables that include multiple <tbody> sections too.

    Fortunately for you and me, introducing this modification to the aforementioned JavaScript function is a pretty straightforward process. It consists basically of adding a few lines of code aimed at checking for the existence of <tbody> tags inside the markup of the zebra table being styled.

    However, listing the improved signature of this function will possibly help you to understand this concept. Therefore, take a look at the following code sample, which shows how the “buildZebraTable()” function is now capable of working seamlessly with zebra tables that contain multiple <tbody> tags within the corresponding markup:


    // improved definition of the '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;

    }

    }

    }


    As you can see, the logic that drives the above “buildZebraTable()” function is capable of dealing directly with (X)HTML tables that include more than one <tbody> header. First, it checks for the existence of these elements in the table, then it iterates over them, and finally it accesses the inner rows in order to style them alternately via the respective “oddrow” and “evenrow” CSS classes. That was really simple to understand, wasn’t it?

    So far, so good. Now that you've hopefully grasped how the previous JavaScript function does its thing, it’s time to move forward and see how it can be included in a sample (X)HTML file. We will attach it to a specific table, as well as to the pertinent CSS styles.

    Are you curious about how this will be done? Jump ahead and read the next few lines. I’ll be there waiting for you.

    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