I would like to be able to "tweak" an HTML table's presentation to add a single feature: when scrolling down through the page so that the table is on the screen but the header rows are off-screen, I would like the headers to remain visible at the top of the viewing area.
This would be conceptually like the "freeze panes" feature in Excel. However, an HTML page might contain several tables in it and I only would want it to happen for the table that is currently in-view, only while it is in-view.
Note: I've seen one solution where the table data area is made scrollable while the headers do not scroll. That's not the solution I'm looking for.
This is really a tricky thing to have a sticky header on your table. I had same requirement but with asp:GridView and then I found it really thought to have sticky header on gridview. There are many solutions available and it took me 3 days trying all the solution but none of them could satisfy.
The main issue that I faced with most of these solutions was the alignment problem. When you try to make the header floating, somehow the alignment of header cells and body cells get off track.
With some solutions, I also got issue of getting header overlapped to first few rows of body, which cause body rows getting hidden behind the floating header.
So now I had to implement my own logic to achieve this, though I also not consider this as perfect solution but this could also be helpful for someone,
Below is the sample table.
Note: The table is wrapped into a DIV with class attribute equal to 'table-holder'.
Below is the JQuery script that I added in my html page header.
and at last below is the CSS class for bit of coloring purpose.
So the whole idea of this logic is to place the table into a table holder div and create clone of that holder at client side when page loaded. Now hide the body of table inside clone holder and position the remaining header part over to original header.
Same solution also works for asp:gridview, you need to add two more steps to achieve this in gridview,
In OnPrerender event of gridview object in your web page, set the table section of header row equal to TableHeader.
And wrap your grid into
<div class="table-holder"></div>
.Note: if your header has clickable controls then you may need to add some more jQuery script to pass the events raised in cloned header to original header. This code is already available in jQuery sticky-header plugin create by jmosbech
That proof of concept you made was great! However I also found this jQuery plugin which seems to be working very well. Hope it helps!
Possible alternatives
js-floating-table-headers
js-floating-table-headers (Google Code)
In Drupal
I have a Drupal 6 site. I was on the admin "modules" page, and noticed the tables had this exact feature!
Looking at the code, it seems to be implemented by a file called
tableheader.js
. It applies the feature on all tables with the classsticky-enabled
.For a Drupal site, I'd like to be able to make use of that
tableheader.js
module as-is for user content.tableheader.js
doesn't seem to be present on user content pages in Drupal. I posted a forum message to ask how to modify the Drupal theme so it's available. According to a response,tableheader.js
can be added to a Drupal theme usingdrupal_add_js()
in the theme'stemplate.php
as follows:It's frustrating that what works great in one browser doesn't work in others. The following works in Firefox, but not in Chrome or IE:
I've made a proof-of-concept solution using jQuery.
View sample here.
I've now got this code in a Mercurial bitbucket repository. The main file is
tables.html
.I'm aware of one issue with this: if the table contains anchors, and if you open the URL with the specified anchor in a browser, when the page loads, the row with the anchor will probably be obscured by the floating header.
Update 2017-12-11: I see this doesn't work with current Firefox (57) and Chrome (63). Not sure when and why this stopped working, or how to fix it. But now, I think the accepted answer by Hendy Irawan is superior.
Check out jQuery.floatThead (demos available) which is very cool, can work with DataTables too, and can even work inside an
overflow: auto
container.