views:

9783

answers:

7

How can I create a table that has its first row and first column both locked, as in Excel, when you activate 'freeze panes'? I need the table to both scroll horizontally and vertically (a lot of solutions for this exist, but only allow vertical scrolling).

So, when you scroll down in the table, the first row will stay put, since it will have the column headings. This may end up being in a thead, or it may not, whatever makes the solution easier.

When you scroll right, the first column stays put, since it holds the labels for the rows.

I am pretty certain this is impossible with CSS alone, but can anyone point me toward a javascript solution? It needs to work in all major browsers.

Thanks in advance!

A: 

How about a solution where you put the actual "data" of the table inside its own div, with overflow: scroll;? Then the browser will automatically create scrollbars for the portion of the "table" you do not want to lock, and you can put the "table header"/first row just above that <div>.

Not sure how that would work with scrolling horizontally though.

matt b
Yeah, that works for vertical scrolling, but not for horizontal scrolling. That is the common solution I mentioned in the question.
pkaeding
A: 

Sort and Lock Table is the only solution I have seen which does work on other browsers than IE. (although this "locked column css" might do the trick as well)

VonC
Yeah, that works for vertical scrolling, but not for horizontal scrolling. That is the common solution I mentioned in the question.
pkaeding
Thank you for the comment. I keep this answer in community mode for reference.
VonC
This solution has a major advantage over some of the others, which is that the table structure is still intact and makes sense. For locking only the headers it is possibly the best possible solution.
Mr. Shiny and New
Thank you for the comment Mr. Shiny and New. May be this deserves a little bump (+1 in vote) after all ? (Note: this is a community post, no rep gain for me)
VonC
A: 

I ran across a site a few weeks back. This is a working example of the first column locked but it is not browser compatible with Firefox. I didn't do a lot of checking around but it seems it only works in IE. There are some notes the author provided along with it that you can read.

Lock the First column: http://home.tampabay.rr.com/bmerkey/examples/locked-column-csv.html

Let me know if you need the Javascript to lock the Table headers too.

Ponchai

Ponchai
A: 

You'd have to test it but if you embedded an iframe within your page then used CSS to absolutely position the 1st row & column at 0,0 in the iframe page would that solve your problem?

David Burrows
Unless I'm misunderstanding you, I don't think that would allow the headings to update when the data is scrolled (ie, scroll the row headings down when the data scrolls down so the row headings still correspond to the appropriate rows).
pkaeding
+6  A: 

Like this? More information is available on the accompanying blog post.

This works! I wish the explanation were more detailed, though - for example, "so it is possible to adjust the size of the cells in the different tables with JavaScript" is all we get about the JS being used.
Nathan Long
The "Like This" link is broken. Does anyone have a working sample of this? Thanks!
Hcabnettek
The link is working now. Perhaps it was just down temporarily?
pkaeding
I still really want to find a solution for this that performs well. The solution in the link in this post is deathly slow when tables start getting big. I am only working with 500 rows and its a hog.
Nicholas Kreidberg
IMHO I think solutions should be posted directly in stackoverflow. What happens if that link gets changed? Then you have link rot. The best would be to post the link and cut and paste the solution.
User
The link is dead ..
Christian Strempfer
+1  A: 

You need two tables, where the first one is an exact overlay over the second one. The second one contains all the data, where the first one just contains the first column. You have to synchronize it's width and depending on the content also the height of it's rows.

Additional to this two tables, you need a third one. That's the first row, which lays exactly between the other two and has to be synchronized in the same way.

You will need absolute positioning here. Next, you would synchronize the scrolling of the data table with the scrolling positions of the head row and first column table.

That works very well in all major browsers, except for one issue: The synchronized scrolling will flutter. To fix that, you need two outher div containers that hold a clone of the content of the header row and the first column. When scrolling vertically, you display the header row clone to prevent fluttering, while you reposition the original in the background. When scrolling horizontally, you would show the first row clone. Same thing here.

Thanks
This is way more complex than I originally wanted to go, but ultimately, it was what I ended up doing (more or less). It seems like this should be a lot easier than it is....
pkaeding
I agree it seems overly complicated but I can not find any other solution for this.
Nicholas Kreidberg
A: 

I've posted my jQuery plugin solution here: http://stackoverflow.com/questions/486576/frozen-table-header-inside-scrollable-div/1533619#1533619

It does exactly what you want and is really lightweight and easy to use.

Mark
This doesn't seem to support locking the first column. Or am I missing something? If not, then this solution is no better than the 'common' solution I mentioned in the question.
pkaeding
I've updated my demo page with locking the first column demo.
Mark