I'm currently working on implementing a sticky table header. The issue I'm facing is that the width of tableHeaderRow
does not match the width of tableHeader
.
Steps taken for creating sticky header:
- Make an Ajax call to populate the table with data
- Save the column widths
- Set
tableHeader
toposition:absolute
- Attempt to set the column widths back into
tableHeader
(although it falls short by about 100 pixels)
Tried approaches:
- Setting
tableHeaderRow
to the expected width - Setting
tableHeaderRow
to100%
width - Removing
padding
andmargin
HTML code snippet:
<table id="table" class="table tablesorter table-responsive table-striped table-hover" style="overflow:auto;border-collapse:collapse;">
<thead id='tableHeader' style="background-color:LightBlue;">
<tr id='tableHeaderRow' >
<th id="col1" class='header'>Column1</th>
<th id="col2" class='header'>Column2</th>
...
</tr>
</thead>
<tbody id='tableBody'></tbody>
</table>
Code to save column widths:
col1Width = $('#col1').width();
col2Width = $('#col2').width();
...
Scroll Event Listener for Sticky Header:
The JavaScript code provided listens for scroll events to make the table headers stick correctly.
If you need any further clarification, feel free to ask. A bootply example has been created to demonstrate the issue, although it seems to work fine there. Any insights on potential CSS plugins causing conflicts will be appreciated. Stay tuned for updates on this matter. If you find my custom sticky table header code useful, please feel free to utilize it.