Home > Articles > Open Source > Ajax & JavaScript

Enrich Your HTML Tables with JavaScript Progressive Enhancement

spacer
  • By Ivan Pepelnjak
  • Sep 7, 2007

📄 Contents

  1. Tabular Headaches
  2. Add the Final Touches
  3. Create Lively Tables
  4. Summary
  • Print
  • + Share This
  • 💬 Discuss
Page 1 of 4 Next >
Like this article? We recommend

Like this article? We recommend

spacer Sams Teach Yourself JavaScript in 24 Hours, 4th Edition

Learn More Buy

Like this article? We recommend

Like this article? We recommend

spacer Sams Teach Yourself JavaScript in 24 Hours, 4th Edition

Learn More Buy
Tabular headaches. Ever had one? Ivan Pepelnjak can relate because he has struggled with table formatting for years. But he has figured out how to use the JavaScript Progressive Enhancement technology to simplify the formatting of web pages and make lively, good-looking HTML tables.

I’ve been struggling with table formatting for years. Although I could use Cascading Style Sheets (CSS) to automate most of my other formatting/design scenarios, tables remained elusive. For example, the simplest requirements (alternate background on even/odd rows and borders between columns) required manually applying CSS classes to half the rows and at least first or last column

NOTE

An in-depth discussion on how you can use various CSS features to minimize the required table formatting can be found in the article "Seven Common CSS Mistakes and How to Avoid Them".

While fixing the server-side scripts to apply the necessary classes to the table rows and cells is usually easy, the tables produced manually remain a huge pain. Just imagine adding a row in the middle of a long table; all the subsequent rows would have to be fixed.

Tabular Headaches

With no obvious short-term CSS advances that would help me solve this problem, I finally gave up and decided to use client-side JavaScript to add the final design touches to the tables. Obviously, the HTML markup itself should produce reasonably acceptable tables with JavaScript progressive enhancement adding the final touches.

To start the project, I defined a new CSS class that would be applied to the whole table and a number of CSS rules that defined the default formatting of cells in the table:

  • The header cells would have gray background and left, right, and bottom borders.
  • All the other table cells would have left and right borders.

Furthermore, I had to define additional classes to be applied with the JavaScript code to enhance the table:

  • The OddRow and EvenRow classes for the table rows
  • The FirstColumn and LastColumn classes for the first and last cell in the row

NOTE

You can even define a separate class for every column to facilitate left/right column alignment.

The resulting CSS rules are included in the following printout and you can view the whole CSS style sheet and the resulting table on my web site.

TABLE.printout { border-collapse: collapse; }

TABLE.printout TD, TABLE.printout TH {
 padding-left:8px; padding-right:8px;
 padding-top: 2pt; padding-bottom: 2pt; 
 border-px; border-right-style: outset; 
 border-left-style: outset; }
 
TABLE.printout TH { 
 border-bottom-style: outset; 
 background-color: #C0C0C0; }

TABLE.printout TR.EvenRow { background-color: #E0E0E0; }
TABLE.printout TR.OddRow {}

TABLE.printout .FirstColumn { border-left-style: none; }

TABLE.printout .LastColumn { border-right-style: none; }

NOTE

Note: As the left and right borders are applied to HTML elements (table cells), you can use CSS classes (the FirstColumn and LastColumn classes) to remove the unwanted attributes (left and right borders). If the borders were defined in a CSS class, the border definitions in the FirstColumn and LastColumn classes would have to contain the !important keyword to override the previous rule.

Page 1 of 4 Next >
  • + Share This
  • 🔖 Save To Your Account

Discussions

comments powered by Disqus

Related Resources

  • Store
  • Articles
  • Blogs
spacer spacer
Microsoft SharePoint 2013 Development LiveLessons (Video Training), Downloadable Video: Learn to Build Today's 10 Most Valuable Applications with Visual Studio 2013 Tools for SharePoint
By Scot Hillier
spacer $119.99
spacer spacer
ASP.NET MVC 5 LiveLessons (Video Training), Downloadable Video
By Jeffrey Palermo
spacer $119.99
My Google Apps
By Patrice-Anne Rutledge, Sherry Kinkoph Gunter
spacer $19.99

spacer See All Related Store Items

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.