I try to set up a responsive table with 2 fixed header in left, so the body is scrollable but the left header is fixed.
I expect the result is like this
My code:
I add position sticky and z index to each head but the second head from left is override the first head.
<div class="table-responsive" style="padding: 10px 0 0 0; overflow: auto;">
<table class="table table-hover">
<col />
<col />
<colgroup span="3"></colgroup>
<thead>
<tr>
<th colspan="2"></th>
<th colspan="31" scope="colgroup" style="text-align: center;">JANUARI</th>
</tr>
<tr>
<th colspan="2" style="width: 10%"></th>
</tr>
</thead>
<tbody>
<tr>
<th rowspan="8" style="position: sticky; left: 0; z-index: 1;" scope="rowgroup">Assy To GFG</th>
<th scope="row" style="position: sticky; left: 0; z-index: 1;">Plan</th>
// data
</tr>
<tr>
<th scope="row" style="position: sticky; left: 0; z-index: 1;">Act</th>
// data
</tr>
<tr>
<th scope="row" style="position: sticky; left: 0; z-index: 1;">F/G</th>
// data
</tr>
<tr>
<th scope="row" style="position: sticky; left: 0; z-index: 1;">Kum P</th>
// data
</tr>
<tr>
<th scope="row" style="position: sticky; left: 0; z-index: 1;">Kum A</th>
// data
</tr>
<tr>
<th scope="row" style="position: sticky; left: 0; z-index: 1;">Kum F/G</th>
// data
</tr>
<tr>
<th scope="row" style="position: sticky; left: 0; z-index: 1;">Blc P</th>
// data
</tr>
<tr>
<th scope="row" style="position: sticky; left: 0; z-index: 1;">Blc D</th>
// data
</tr>
</tbody>
</table>
</div>
4
Answers
You can achieve this by using
JQuery DataTable
(as you tagged the question with jquery I will assume you could accept this kind of answer)Simply add the jquery datatable plugin and the following script. It will be better if you write an
Id
for the tableFunctional example and library:
https://datatables.net/extensions/fixedcolumns/examples/initialisation/left_right_columns.html
When you download or select a CDN, don’t forget check the option "Fixed Columns"
or you can use the following:
You definitely need to use scripts here.
You can do without plugins. The bottom line is that you need to set the same height for the rows of the tables. Something like this 👇:
Here is a working demo-