JSFiddle - React, Tailwind, and code Playground
by homer2
HTML
<table class="tableWith-FloatingHeader">
<thead>
<tr><td>head</td></tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
JavaScript
jQuery(function(){
for(var i = 0; i<100; i++){
$("#tbody").append("<tr><td>"+i+"</td></tr>");
}
});
$("table.tableWith-FloatingHeader").each(function() {
$(this).wrap("<div class=\"divTableWithFloatingHeader\" style=\"position:relative\"></div>");
var originalHeaderRow = $("thead", this);
originalHeaderRow.before(originalHeaderRow.clone());
var clonedHeaderRow = $("thead", this);
clonedHeaderRow.addClass("tableFloatingHeader");
originalHeaderRow.addClass("tableFloatingHeaderOriginal");
if(originalHeaderRow.hasClass("tableFloatingHeader")){
originalHeaderRow.removeClass("tableFloatingHeader");
}
clonedHeaderRow = $('.tableFloatingHeader');
clonedHeaderRow.css("position", "absolute");
clonedHeaderRow.css("top", "0px");
clonedHeaderRow.css("left", $(this).css("margin-left"));
// originally visibility hidden only was used but it didn't work for me so I had to use display:none
clonedHeaderRow.css("visibility", "hidden");
clonedHeaderRow.css("display", "none");
var tableBody = $('tbody', this);
tableBody.css('display', 'table');
});
UpdateTableHeaders();
$(window).scroll(UpdateTableHeaders);
$(window).resize(UpdateTableHeaders);
// since my table's width could be changing on click to the td (expand/collapse of td leads to change in width so to fix the width for the head and body UpdateTableHeader function is called.
$("table.tableWith-FloatingHeader").undelegate("td", "click");
$("table.tableWith-FloatingHeader").delegate("td", "click", function() {
UpdateTableHeaders();
});
// functions **
// function to fix the table head's width and it's visibility
...