Chromatable JQuery Plugin

A couple of days ago I wrote a lengthy tutorial about creating scrolling tables with fixed headers. I thought I could take the exercise a step further by creating a jQuery plugin to keep the code contained in one location and allow users to call the "chromatable()" method on any table, or many tables on the same page.

Check out this header Look here's another one Wow, look at me!
Some content goes in here Praesent vitae ligula nec orci pretium vestibulum Maecenas tempus dictum libero
Quisque in wisi quis orci tincidunt fermentum Mauris aliquet mattis metus Etiam eu ante non leo egestas nonummy
Some content goes in here Praesent vitae ligula nec orci pretium vestibulum Maecenas tempus dictum libero
Quisque in wisi quis orci tincidunt fermentum Mauris aliquet mattis metus Etiam eu ante non leo egestas nonummy
Some content goes in here Praesent vitae ligula nec orci pretium vestibulum Maecenas tempus dictum libero
Quisque in wisi quis orci tincidunt fermentum Mauris aliquet mattis metus Etiam eu ante non leo egestas nonummy
Some content goes in here Praesent vitae ligula nec orci pretium vestibulum Maecenas tempus dictum libero
Quisque in wisi quis orci tincidunt fermentum Mauris aliquet mattis metus Etiam eu ante non leo egestas nonummy
Some content goes in here Praesent vitae ligula nec orci pretium vestibulum Maecenas tempus dictum libero
Quisque in wisi quis orci tincidunt fermentum Mauris aliquet mattis metus Etiam eu ante non leo egestas nonummy
Some content goes in here Praesent vitae ligula nec orci pretium vestibulum Maecenas tempus dictum libero
Quisque in wisi quis orci tincidunt fermentum Mauris aliquet mattis metus Etiam eu ante non leo egestas nonummy
Some content goes in here Praesent vitae ligula nec orci pretium vestibulum Maecenas tempus dictum libero
Quisque in wisi quis orci tincidunt fermentum Mauris aliquet mattis metus Etiam eu ante non leo egestas nonummy
Some content goes in here Praesent vitae ligula nec orci pretium vestibulum Maecenas tempus dictum libero
Quisque in wisi quis orci tincidunt fermentum Mauris aliquet mattis metus Etiam eu ante non leo egestas nonummy
Some content goes in here Praesent vitae ligula nec orci pretium vestibulum Maecenas tempus dictum libero
Quisque in wisi quis orci tincidunt fermentum Mauris aliquet mattis metus Etiam eu ante non leo egestas nonummy

How to use?

To make the plugin work, you'll need to add the following to the head of your HTML:

  1. <link href="css/style.css" rel="stylesheet" type="text/css" />
  2. <script type="text/javascript" src="jquery.chromatable.js"></script>

Then, call the chromatable() method on any table within your code, for example:

  1. $(document).ready(function(){
  2. $("#yourTableID").chromatable();
  3. });

You can modify the table's height and width by adding the following lines:

  1. $("#yourTableID2").chromatable({
  2.  
  3. width: "900px",
  4. height: "400px",
  5. scrolling: "yes"
  6.  
  7. });

For the first release of this plugin, the table width is limited to being calculated as a pixel dimension, percentage values will not work. Version 1.2.0 supports table widths set to a fixed pixel dimension, auto, or 100%. You may see unexpected formatting results if you specify the table too narrow to accommodate your columns.

In the download package, you will see an example.html file of how to setup the chromatable properly, in most cases it should work perfectly out of the box. That's it, you should be ready to rock with your new Chromatables! Please feel free to send me your feedback or questions regarding implementation. This is one of my first plugins I've written for public use so I'm sure folks might have some insight on how to improve upon the idea for future releases.

Download

Latest source

Known problems

Some peeps have reported issues in IE6 and Opera. Although I don't plan to support IE6 in the near future, I will be looking into a future release to fix problems in Opera.

Speak your mind

32

Comments

Anonymous's picture

Would it be possible to use this on all tables of a given class, instead of by table name? Reason being I generate table names at runtime. There could theoretically be any number of tables on a page, however they would all be of the same class.

Dunsworth's picture

I'm using this on an internal site for work, love it, looks great, except one detail (We use IE8) When I work in VS2008, I'm getting about a 5 pixel gap on the th's at the far right side. If you are interested, I can send a screen shot of what I have and show you.

I know it's in the css somewhere, but can't find it right now.

Sachin's picture

In IE7 scrollbar is not visible in second table when the page is first opened,but data gets scrolled.But then get displayed properly when "Expand Table" is clicked.

Spencer Dillard's picture

One enhancement suggestion: rather than assuming a width for the scroll bar of 17px, you may want to calculate a scrollbar width. I do it using an off-screen check:

function getScrollbarWidth() {
var div = $('

Anonymous's picture

Thanks for your library. =)
Unfortunately, I think it's not working with master pages. Do you have any suggestions ?

Thanks in advance.

sorglos's picture

Thank you for the resource code and letting me know it can do it like that ,thank you

Paul Speranza's picture

@Tarek To get an ASP.Net gridview to render the headers over ride the DataBound event. So, for a grid with an Id of MyGrid do the following:

protected void grdMyGrid_DataBound(object sender, EventArgs e) {
MyGrid.UseAccessibleHeader = true; if (MyGrid.Rows.Count != 0) {
MyGrid.HeaderRow.TableSection = TableRowSection.TableHeader;
MyGrid.FooterRow.TableSection = TableRowSection.TableFooter;
}
}

Rick Jackson's picture

Hello Paul,

I have shown how to render the Headers inside the gridview. I think you have worked out with asp.net integration, So can u just put the sample code out. Cause i am not able to work it out,

Thanks
Rick

GBloggt's picture

Wow. I'm impressed by this demo. Now i'll go and figure out, how to use this plugin.

Keep up the good work.

Tarek's picture

Question:
Is there is a way to do it without the thead?

Some jsps, or asp.net, when spitting out html tables,
they dont generate the thead tag..

Is there is a way to override that? may be reference the table header with a class?

Let me know, and thanks for all your efforts and your help.

Shane Graber's picture

If you do a print preview and/or print the page the table is still in its scroll state and any of the data that's not shown will not print. When someone prints the page, the plugin should turn off the scrolling so the printout contains the entire table. Other than that it looks pretty interesting.

Shane Graber's picture

Excellent! Now all I need is to figure out how to merge tablesorter, and chromatable to have what I'm looking for, a sortable table with a fixed header.

Jared's picture

I would really like to see this feature. I have a a lot of data to display.

Julius Dietz's picture

I would like to see horizontal scrolling feature as well. It should allow the first column to stay fixed, optionally.

Julius Dietz's picture

I have the same problem with Firefox 3.5.5, the header disappear as I scroll down

Julius Dietz's picture

It doesn't work on Opera 10. Clever technique... duplicating the header and use CSS to maintain it's position.
Is it what Google uses on it's Adwords interface?

J.D.'s picture

How well does Chromatable hold up when the table is initially hidden or, if the table contents (cell widths) change dynamically? Will your plugin support the sizing changes okay? I have had issues with other JQuery plugins that do not handle tables very well where the cell contents change causing the width of the table go grow.