I came up with a solution that combines the two previously mentioned. It uses jQuery and two tables: one for the header and one for the content. The header table is 100% wide without setting the column width. At the bottom of the content table there is a row defined to match the header table with the specified column widths. This row is hidden so that it is not displayed, but retains the width of the column.
In this example, I gave the header line ID 'Header1', and the bottom line ID 'Header2'. I also wrapped the content table inside a div with the id "scrollTable".
I set the styles in my CSS file for the scrollTable ID, see below:
#scrollTable { height:250px; overflow-x:hidden; overflow-y:scroll; }
Now for the jQuery part. Basically, what I'm doing here is taking the width of the columns of the bottom rows and setting the header columns. I am stretching the width of the last column of the title so that it fits into the top of the scroll bar. See the following code:
$(document).ready(function(){ var maxWidth = $('#Header1').width(); // Get max row Width $('#Header2 th').each(function(i) { // Set col headers widths to to match col widths var width = $(this).width(); $('#Header1 th').eq(i).width(width); }); var blankSpace = maxWidth - $('#Header1').width(); // Calculate extra space $('#Header1 th:last').width( $('#Header1 th:last').width() + blankSpace ); // Stretch last header column to fill remaining space });
I successfully tested this on IE 6, 7 and 8, Firefox 3.0.1.4, Chrome 3.0.195.25, Opera 10 and Safari 3.2.2 on Windows XP.
Zulucap
source share