I have a table which has a header row, but also a header column and a total column with several columns in between.
Something like this:
Name Score 1 Score 2 ... Total
--------------------------------------
John 5 6 86
Will 3 7 82
Nick 7 1 74
The entire table is defined inside a fixed-width scrollable div because there are likely to be a large number of "Score" rows and I have a fixed-width page layout.
<div id="tableWrapper" style="overflow-x: auto; width: 500px;">
<table id="scoreTable">
...
</table>
</div>
What I would like is for the first (Name
) and last (Total
) columns to remain visible while the inner columns scroll.
Can anyone help me with this?
Edit: I mean horizontal scrolling only - changed to specify that.
Update: I've solved this problem for myself and have posted the answer below. Let me know if you need any more information - this was a bit of a pain to do and I'd hate for someone else to have to rewrite everything.
To freeze the row/column we can use a simple HTML table and CSS. HTML: In HTML we can define the header row by <th> tag or we can use <td> tag also. Below example is using the <th> tag. We also put the table in DIV element to see the horizontal and vertical scrollbar by setting the overflow property of the DIV element.
By setting postion: sticky and top: 0, we can create a fixed header on a scroll in HTML tables.
It is possible to create a table, which has a fixed left column and a scrollable body. For that, you'll need to use some CSS. You can use the position property set to “absolute” for the first column and specify its width. Then use the overflow-x property set to “scroll” for the entire table.
Suppose we want to add a scroll bar option in HTML, use an “overflow” option and set it as auto-enabled for adding both horizontal and vertical scroll bars. If we want to add a vertical bar option in Html, add the line “overflow-y” in the files.
Can I propose a somewhat unorthodox solution?
What would you think about placing the 'total' column after the 'name' column, rather than at the very end? Wouldn't this avoid the requirement for only a portion of the table to scroll?
It's not exactly what you're asking for, but perhaps it is a sufficient solution, given that the alternative would be pretty messy. (Placing the 'total' and 'name' columns outside of the table, for instance, would create alignment problems when not all rows are of equal height. You could correct this with javascript but then you'd be entering a whole new world of pain).
Also from a UI perspective, it may be that 'name' and 'total' are the most important data, in which case it would make sense to put them together, followed by a sort of 'breakdown' of the total. Of course, we seem to have an intuition that a 'total' should come after its constituent parts, but I don't think it would cause too much confusion to the user if the order were reversed like this (though this is a question for you, based on your product and your users).
Anyway, something to consider.
EDIT:
Here are some more unorthodox solutions, now that I think I understand your intentions a bit better:
Anyway you get the idea. Perhaps it is better to look for a UI solution than a contorted markup solution. Ultimately I would be questioning how important it is to present so much data to the user at once, that a portion of it needs to scroll in a particular fashion for the data to be readable. Perhaps you're building a spreadsheet app, and you really do need to display a 100x100 matrix in a single view. If not, you could surely come up with more creative ways than I have to split up the results.
Take a look at this jQuery plugin:
http://fixedheadertable.com/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With