Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
181
rated 0 times [  188] [ 7]  / answers: 1 / hits: 51095  / 11 Years ago, fri, july 19, 2013, 12:00:00

I have like 15 columns in the table. I want to scroll both horizontal and vertical and header should be fixed when I scroll vertical. I have tried various examples but no luck. All I see header columns are not aligning with data. Column width are not fixed as number of columns vary based on the user selection



Please guide me to the correct example.


More From » html

 Answers
145

what you could do is a bit of visual trick to achieve this,
use two div tags



<div class=Headers>
<table class=NewHeader>
<tr>
</tr>
</table>
</div>
<div class=Table>
<table class=MyTable>
<tr>
<th>
</th>
...
</tr>
<tr>
<td>
</td>
...
</tr>
</div>


now with a bit of JavaScript or JQuery you can get the th, set its width to match the cell width, and move the th cell to the Headers table



$(document).ready(function(){
var counter = 0;
$(.MyTable th).each(function(){
var width = $('.MyTable tr:last td:eq(' + counter + ')').width();
$(.NewHeader tr).append(this);
this.width = width;
counter++;
});
});


now only is left to do is to style the div Table with overflow, so now if you would scroll the second table the header will remain in place, i used jquery to simplify the readability, but can be done in JavaScript is same way



Live Demo



Example with automatic vertical scroll body and header


[#76870] Thursday, July 18, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
taylert

Total Points: 627
Total Questions: 91
Total Answers: 108

Location: Mayotte
Member since Mon, Sep 12, 2022
2 Years ago
taylert questions
;