JavaScript
  Home arrow JavaScript arrow Page 2 - Building Zebra Tables with CSS and JavaScr...
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 
Sun Developer Network 
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

Building Zebra Tables with CSS and JavaScript
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2008-06-11

    Table of Contents:
  • Building Zebra Tables with CSS and JavaScript
  • Building zebra tables with a server-side scripting language
  • Creating a zebra table using CSS and some basic structural markup
  • Creating a zebra table by styling only its even rows

  • 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


    Building Zebra Tables with CSS and JavaScript - Building zebra tables with a server-side scripting language


    (Page 2 of 4 )

    As I explained in the introduction, there are different approaches that can be taken to building zebra tables, including both client- and server-side techniques. In this particular case, I'm going to demonstrate how to create the tables in question by using a basic PHP function. Then, with that example digested, you'll learn how to build them using a combination of CSS and the corresponding structural markup.

    That being said, pay attention to the signature of the following user-defined PHP function, which is tasked with building rudimentary zebra tables. Here it is: 


    // example on building a zebra table with a server-side scripting language like PHP


    // define 'buildZebraTable()' function (generates the zebra table dynamically)


    function buildZebraTable($oddRows='#cccccc',$evenRows='#eeeeee',$numRows=10){

    $evenFlag=false;

    $html='<table><tbody>'."n";

    for($i=0;$i<$numRows;$i++){

    $bgColor=!$evenFlag?$evenRows:$oddRows;

    $html.='<tr bgcolor="'.$bgColor.'"><td>Content for cell goes here</td></tr>'."n";

    $evenFlag=!$evenFlag;

    }

    $html.='</tbody></table>';

    return $html;

    }


    Even if you're not familiar with PHP, I'm pretty sure that you'll find the logic implemented by the previous "buildZebraTable()" function extremely easy to understand and use. As you can see, the function takes up a few incoming parameters, such as the background colors of the even and odd rows, as well as the number of rows that the table will contain.

    With all of these arguments available, the function simply uses a PHP loop to build the markup of the zebra table. It finalizes its execution by returning the whole HTML to client code. Quite easy to understand, isn't it?

    Having explained how the previous "buildZebraTable()" PHP function does its thing, here's an example that demonstrates how to use it, accompanied by an illustrative screen capture:


    <?php

    // build zebra table

    echo buildZebraTable();

    ?>



    As you'll realize, building zebra tables using a custom PHP function like the one shown a few lines above is actually a straightforward process that can be extended to other server-side programming languages with relative ease. Of course, the previous function is rather useless on its own, since it populates the pertinent table rows with static data. So its signature should eventually be modified to work with records pulled directly from a database.

    Nevertheless, my mission has been largely accomplished, since I showed you how to build and display zebra tables utilizing a server-side approach. But the tables in question can also be constructed by way of conventional (X)HTML markup and some simple CSS styles. So in the section to come, I'll be showing you the complete details of this interesting process.

    Do you see the link that appears below? Great! Click on it and keep reading.

    More JavaScript Articles
    More By Alejandro Gervasio


       · Zebra tables are an elegant way to display tabular data on web pages. Though, it’s...
       · Where is the Javascript ?
       · Thank you for commenting on my article. As mentioned in the last section of this...
     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT