JavaScript
  Home arrow JavaScript arrow Page 4 - 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 - 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.

       · 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

    - 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
    - Active Client Pages at the Server
    - ACP Tab Web Page







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT