views:

15131

answers:

13

Does anyone know a CSS/Javascript technique to display a long html table such that the column headers stay fixed on-screen and the first coloumn stay fixed and scroll with the data.

I want to be able to scroll through the contents of the table, but to always be able to see the column headers at the top and the first column on the left.

If there was a jquery plugin that would be great! If it helps the only browser I care about is firefox.

A: 

I know you can do it for MSIE and this limited example seems to work for firefox (not sure how extensible the technique is).

MarkusQ
First linked didn't work for me. Second one does now show how to produce fixed first column.
allyourcode
A: 

You could have 3 frames. One frame for the headers, one for the first column, and a third for the rest of the data.

The third frame can set the overflow to auto. This will bring scrollbars in when necessary.

Kevin Crowell
this (alone) won't keep things lined up
allyourcode
A: 

The first column has a scrollbar on the cell right below the headers

<table>
    <thead>
     <th> Header 1</th>
     <th> Header 2</th>
     <th> Header 3</th>
    </thead>
    <tbody>
     <tr>
      <td>
       <div style="width: 50; height:30; overflow-y: scroll"> 
        Tklasdjf alksjf asjdfk jsadfl kajsdl fjasdk fljsaldk 
        fjlksa djflkasjdflkjsadlkf jsakldjfasdjfklasjdflkjasdlkfjaslkdfjasdf
       </div>
      </td>
      <td>
       Hello world
      </td>
      <td> Hello world2
     </tr>
    </tbody>
</table>
ferrari fan
I think you've misunderstood what's being asked. We want to be able to scroll to different cells in a (large) table.
allyourcode
+7  A: 

If what you want is to have the headers stay put while the data in the table scrolls vertically, you should implement a <tbody> styled with "overflow-y: auto" like this:

<table>
  <thead>
    <tr>
      <th>Header1</th>
       . . . 
    </tr>
   </thead>
   <tbody style="height: 300px; overflow-y: auto"> 
     <tr>
       . . .
     </tr>
     . . .
   </tbody>
 </table>

If the <tbody> content grows taller than the desired height, it will start scrolling. However, the headers will stay fixed at the top regardless of the scroll position.

levik
thanks, that helps with the fixed header part but what about the fixed column?
koogunmo
overflow-y is ms specific. You'll have to use a plain overflow:auto.
Thomas Ahle
Would it be possible to do two colgroups and give the second one overflow:auto, to achieve the fixed first col?
Thomas Ahle
@levik: does not work in almost all browsers at least using HTML 5 DOCTYPE in standrad mode
Marco Demajo
A: 

YUI DataTable

I don't know if YUI DT has this feature but I won't be surprised if it does.

ksuralta
Thanks I took a look at that. It does have a scrolling table but that does have a fixed header but not the fixed coloumn that I need.http://developer.yahoo.com/yui/examples/datatable/dt_fixedscroll.html
koogunmo
yeah, I couldn't find that either from their list of examples.
allyourcode
+2  A: 

You might be looking for this.

It has some known issues though.

teriz
How can i download this solution ?
penguru
+8  A: 

http://fixed-header-using-jquery.blogspot.com/ check this

pranav
+1 for neat solution, but that is a sad blog....
BigBlondeViking
that is exactly what koogunmo asked... cool!
robnardo
+5  A: 

Working example of link posted by pranav:

http://acatalept.com/common/test/fixed-table.html

FYI:

Tested in IE 6, 7, & 8 (compatibility mode on or off), FF 3 & 3.5, Chrome 2.

Not screen-reader-friendly (headers aren't part of content table).

EDIT: fixed link to jQuery library, which stopped working at some point after my original post.

acatalept
doesn't work for me in winxp ie8 or ff3.6
allyourcode
The link to the jQuery library hosted on jQuery.com changed, and their redirect was incorrect... so it stopped working in ALL browsers.I've fixed the link to point to the file on Google's servers instead - hopefully that one should be more reliable.
acatalept
+1  A: 

I see this, although an old question, is a pretty good place to plug my own script:

http://code.google.com/p/js-scroll-table-header/

It just works with no configuration and is really easy to setup.

Raveren
A: 

Here is a good jquery plugin, working in all browsers !

You have a fixed header table without fixing his width...

Check it : http://www.tablefixedheader.com/

bln
+1  A: 

In this answer there is also the best answer I found to your question:

http://stackoverflow.com/questions/673153/html-table-with-fixed-headers/673162#673162

and based on pure CSS.

Marco Demajo
A: 

Hi Guys,

I have created something which has fixed header, fixed footer, fixed left column and also fixed right column. This only works fine in IE. As most of the users are still using IE this can be helpful. Please find the code here in Scrollable Table. Please let me your suggestions.

Meanwhile I am working to fix columns in other browser. I will keep you posted. :-)

Shahib
This isn't a great solution, IE has stopped supporting CSS expressions: http://msdn.microsoft.com/en-us/library/cc304082%28VS.85%29.aspx#expressions
Homer
A: 

This blog has good information about scrollable tables. It has different types of scrollable table.

http://s7u.blogspot.com

Regards, Shahib

Shahib