HOME FORUM ARTICLES TUTORIALS SCRIPTS LINKS NEWS MENTORS TOOLS REGISTER

CSS for Dynamic Data Table Presentation

A tutorial by COBOLdinosaur

The table is the most misused and abused tag in HTML. Tables are not intended to be used for layout. They are specified the way they are for optimization for data table presentation. They do not have to be boring grids. Using CSS for Dynamic effects is a powerful and elegant tool for the developer.

CSS, Scripting and the methods available in the DOM, give the developer almost unlimited flexibility, and the opportunity to enhance the user experience. Consider this. Not all users have the same taste in color selection. Let's start by offering some simple options. A table with alternating row colors is simple. A couple of CSS declarations:

Instead of coding the classes on every row in the table and making maintenance difficult when we have to add or remove rows; we will just add the styling dynamically with scripting:

BUT... wait a minute! A web page should be about the user. Why not give them some options? With a tiny extra bit of coding we can give them:

Select a style:

Project Name Description Start Date End Date Required Skills
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA

All it took was a little extra CSS to define a set of row pairs instead of one:

We added some buttons to fire the switch:

[The buttons could also be text or image links] The final step is a slight modification of the script:

Shall we stop there, or can we make this little data table even easier to use and more appealing to the user?

We could allow them to highlight a column. An Icon in the headings and instant highlighting. While we are at it, we can use a mouseover event to show the current row. One final touch will be to dull the button for the currently active style.

What we will have is this:

Project Name Description Start Date End Date Required Skills
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA

AND... we are good to go. The script ends up looking like this:

There are all kinds of possibilities and we could end up with a 20 page article if we explored them all. Much better than that is experimenting. You have the basics and code to work from. Try different CSS, tagging or scripting. The most enjoyable part of web development is the experimentation and moments of personal discovery.

Play with this table. Have some fun. Be glad you are a developer who gets to play on the web every day.

A note on "best practices".

The code used in the samples is presented the way it is to enhance understanding. The use of javascript in this way can have long term negative impacts on maintenance. In future articles on this topic I will extend the exploration of presentation methods for data tables, and look at "best practices", advanced uses of the DOM methods, and stylesheet object manipulation. The samples here are intended as a starting point. If they are helpful then check back for the next installment where we will extend and improve on what we have done so far.

© Copyright COBOLdinosaur 1999-2007

post to Dzone Digg this! Add to del.icio.us Googleize this Add to reddit Save to myYahoo Add to furl Add to Netvouz! Spurl this! Add to Linkroll! Save to Simpy Give if thumbs up on StumbleUpon Save to Blinklist Add to Tektag Save to Bibsonomy Submit to Tweako
Search ERT on the Tools Page

Did you know? You can discuss this article with the mentor who wrote it and others interested in the topic? You are invited to join the discussion with Go to the forum

Got a technical article or tutorial you want to publish on the Internet? Join Go to the forum in the Round Table Forum and let the Mentors know what you have. If it meets ERT standards, is factual and can help ERT visitors, then ERT Mentors and Editors can help you (without charge) polish your offering so it can be published and promoted by ERT. An article published on ERT may be read by as many as 10,000 visitors a week; promoting you, your site, and your ideas. Please note ERT does not publish re-prints; promotional handouts, or pieces consisting mainly of links. So original technical content only please. If you prefer you can email the Editor