Objectives:
- Column width of table header and table body should be same.
- Table header to be fixed on vertical scroll.
- Table header to be moved on horizontal scroll.
- Header to be displayed inside scroll-able division of HTML.
Problem statements:
- Table header does not get fixed on vertical scroll.
- Header displayed out of scroll-able division of HTML(When header position is fixed).
Have seen many posts, could not find the solution to this particular problem statement.
Have created two JSFiddles to demonstrate the problem statements,
Fiddle01: Solution works good as a separate module. JSFiddle01
Fiddle02: Solution does not work when its integrated with other divs. JSFiddle02
Help would be very much appreciated.
$(function() {
var $window = $(window);
var $table = $('table');
var $head = $table.find('thead');
var $body = $table.find('tbody');
var $headTds1 = $head.find('tr').eq(0).find('th');
var $headTds2 = $head.find('tr').eq(1).find('th');
var $bodyTds = $body.find('tr').eq(0).find('td');
var tableWidth = $table.outerWidth();
// console.log("Width:"+tableWidth);
var $tableNew = $('<table/>');
var cl = 0; // colspan total length
var cc = 0; // colspan count
$table.css({ width: tableWidth });
$tableNew
.attr("class", $table.attr("class"))
.css({ width: tableWidth });
$head.css({ background: '#fff' });
$.each($headTds1, function(index, value) {
var $headTd = $(value);
var colspan = $headTd.attr('colspan') || 0;
if (colspan) {
while (colspan) {
addwidth($($headTds2[cl]), $($bodyTds[index + cl - cc]));
colspan--
cl++
}
cc++;
} else {
addwidth($headTd, $($bodyTds[index + cl - cc]));
}
});
function addwidth($headTd, $bodyTd) {
var headTdwidth2 = $headTd.outerWidth();
var bodyTdwidth2 = $bodyTd.outerWidth();
var width2 = headTdwidth2 > bodyTdwidth2 ? headTdwidth2 : bodyTdwidth2;
$bodyTd.css({ 'width': width2 });
$headTd.css({ 'width': width2 });
var headTdwidth = $headTd.width();
var bodyTdwidth = $bodyTd.width();
var width = headTdwidth > bodyTdwidth ? headTdwidth : bodyTdwidth;
$bodyTd.html('<div style="width: ' + width + 'px">' + $bodyTd.html() + '</div>');
$headTd.html('<div style="width: ' + width + 'px">' + $headTd.html() + '</div>');
}
$head.appendTo($tableNew);
$tableNew.insertBefore($table);
// $table.css({ 'margin-top': $tableNew.height() });
$tableNew.css({ position: 'fixed' });
$window.scroll(reLeft);
$window.resize(reLeft);
function reLeft() {
$tableNew.css({ left: $table.offset().left - $window.scrollLeft() });
}
});
from JQuery: Table header to be fixed on vertical scroll and to be moved on horizontal scroll, within scroll-able division
No comments:
Post a Comment