OK I understood:
You need to wrap the table in two DIVs:
<div class="outerDIV"> <div class="innerDIV"> <table></table> </div> </div>
CSS for DIVs:
.outerDIV { position: relative; padding-top: 20px; //height of your thead } .innerDIV { overflow-y: auto; height: 200px; //the actual scrolling container }
The reason is because you basically make the inner DIV scrollable and pull the THEAD out of it, sticking it to the outer DIV.
Now bind thead to an external thead by specifying it
table thead { display: block; position: absolute; top: 0; left: 0; }
tbody should have display: block .
Now you will notice that scrolling works, but the widths are completely confused. It was Javascript. You can choose how you want to assign it. I myself gave TH a fixed width in the table and built a simple script that takes the width and assigns them to the first TD row in tbody .
Something like this should work:
function scrollingTableSetThWidth(tableId) { var table = document.getElementById(tableId); ths = table.getElementsByTagName('th'); tds = table.getElementsByTagName('td'); if(ths.length > 0) { for(i=0; i < ths.length; i++) { tds[i].style.width = getCurrentComputedStyle(ths[i], 'width'); } } } function getCurrentComputedStyle(element, attribute) { var attributeValue; if (window.getComputedStyle) {
With jQuery, of course, that would be a lot easier, but so far I have not been allowed to use a third-party library for this project.
ProblemsOfSumit
source share