Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

MooTools Zebra Tables Class

DZone's Guide to

MooTools Zebra Tables Class

· Web Dev Zone ·
Free Resource

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Tabular data can oftentimes be boring, but it doesn't need to look that way! With a small MooTools class, I can make tabular data extremely easy to read by implementing "zebra" tables -- tables with alternating row background colors.

The CSS

.highlight		{ background:#d5fcdc; }
.even { background:#fff; }
.mo { background:#e3f1fb; }
.odd { background:#eee; }
.zebra th { padding:5px; background:#ddd; border-bottom:1px solid #999; text-align:left; font-weight:bold; }
.zebra td { padding:5px 20px 5px 5px; border-bottom:1px solid #ddd; }

The above CSS is extremely basic. I've styled the <TH> tag so that it stands out from table rows. There are four states of rows in my zebra table: highlighted (or "clicked on"), regular even and odd, and a mouseover state -- these are represented by the "even", "mo", "highlight", and "odd" classes. I've added padding and a bottom border to the <TD>'s for presentation.

The XHTML

<table class="zebra" cellpadding="0" cellspacing="0">
<tr>
<th>Award</th>
<th>Actor</th>
<th>Film</th>
</tr>
<tr>
<td>Actor In A Leading Role</td>
<td>Daniel Day-Lewis</td>
<td>There Will Be Blood</td>
</tr>
<tr>
<td>Actress In A Leading Role</td>
<td>Marion Cotillard</td>
<td>La Vie en Rose</td>
</tr>
<tr>
<td>Actor In A Supporting Role</td>
<td>Javier Bardem</td>
<td>No Country For Old Men</td>
</tr>
<tr>
<td>Actress In A Supporting Role</td>
<td>Tilda Swinton</td>
<td>Michael Clayton</td>
</tr>
<tr>
<td>Directing</td>
<td>Joel Coen and Ethan Coen</td>
<td>No Country For Old Men</td>
</tr>
<tr>
<td>Writing</td>
<td>Diablo Cody</td>
<td>Juno</td>
</tr>
</table>

The above table is a simple, standard table. The only detail of note is that this table is given the "zebra" class, which signals to MooTools that this table should be zebra-ized.

The Javascript Class

/* classes */
var ZebraTables = new Class({
//initialization
initialize: function(table_class) {

//add table shading
$$('table.' + table_class + ' tr').each(function(el,i) {

//do regular shading
var _class = i % 2 ? 'even' : 'odd'; el.addClass(_class);

//do mouseover
el.addEvent('mouseenter',function() { if(!el.hasClass('highlight')) { el.addClass('mo').removeClass(_class); } });

//do mouseout
el.addEvent('mouseleave',function() { if(!el.hasClass('highlight')) { el.removeClass('mo').addClass(_class); } });

//do click
el.addEvent('click',function() {
//click off
if(el.hasClass('highlight'))
{
el.removeClass('highlight').addClass(_class);
}
//click on
else
{
el.removeClass(_class).removeClass('mo').addClass('highlight');
}
});

});
}
});

The class accepts one parameter, which is the class given to tables that should be Zebra-ized. Upon initialization, the class cycles through each table row. During the row looping, the row is given its odd or even CSS class, and a listener is added to the row to highlight the row upon mouseover. The above javascript could be placed into an external javascript file.

The Javascript Implementation

/* do it! */
window.addEvent('domready', function() {
var zebraTables = new ZebraTables('zebra');
});

To implement ZebraTables, all you need to do is add the above code to any given page.

Click here to experience beautifully formatted tabular data!

Do you have any suggestions for my zebra tables? Let me know!

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}