Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion demo/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,10 @@
<table class="fancyTable" id="myTable02" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>Browser</th>
<th rowspan="2">Browser</th>
<th colspan="5">This is merged column</th>
</tr>
<tr>
<th>Visits</th>
<th>Pages/Visit</th>
<th>Avg. Time on Site</th>
Expand Down
234 changes: 196 additions & 38 deletions jquery.fixedheadertable.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,26 +561,16 @@
tbody: {},
tfoot: {},
border: 0
},
borderCollapse = 1;
};

if (settings.borderCollapse == true) {
borderCollapse = 2;
}
tableProp.border = ($obj.find('th:first-child').outerWidth() - $obj.find('th:first-child').innerWidth())
/ settings.borderCollapse ? 2 : 1;

tableProp.border = ($obj.find('th:first-child').outerWidth() - $obj.find('th:first-child').innerWidth()) / borderCollapse;
tableProp.thead = helpers._getColumnsWidth($obj.find('thead').children('tr'));

$obj.find('thead tr:first-child > *').each(function(index) {
tableProp.thead[index] = $(this).width() + tableProp.border;
});
tableProp.tfoot = helpers._getColumnsWidth($obj.find('tfoot').children('tr'));

$obj.find('tfoot tr:first-child > *').each(function(index) {
tableProp.tfoot[index] = $(this).width() + tableProp.border;
});

$obj.find('tbody tr:first-child > *').each(function(index) {
tableProp.tbody[index] = $(this).width() + tableProp.border;
});
tableProp.tbody = helpers._getColumnsWidth($obj.find('tbody').children('tr'));

return tableProp;
},
Expand All @@ -590,32 +580,200 @@
* Fix widths of each cell in the first row of obj.
*/
_setupClone: function($obj, cellArray) {
var $self = $obj,
selector = ($self.find('thead').length) ?
'thead tr:first-child > *' :
($self.find('tfoot').length) ?
'tfoot tr:first-child > *' :
'tbody tr:first-child > *',
$cell;

$self.find(selector).each(function(index) {
$cell = ($(this).find('div.fht-cell').length) ? $(this).find('div.fht-cell') : $('<div class="fht-cell"></div>').appendTo($(this));

$cell.css({
'width': parseInt(cellArray[index], 10)
var selector = ($obj.find('thead').length)
? 'thead tr'
: ($obj.find('tfoot').length)
? 'tfoot tr'
: 'tbody tr';

helpers._trCellIterator(
$obj.find(selector),
{
cell: function(context) {
if (typeof cellArray[context.colIndex] !== 'undefined'
&& cellArray[context.colIndex].colspan == context.colspan
) {

var $cell = ($(this).find('div.fht-cell').length)
? $(this).find('div.fht-cell')
: $('<div class="fht-cell"></div>').appendTo($(this));

$cell.css({
'width': parseInt(cellArray[context.colIndex].width, 10)
});

/*
* Fixed Header and Footer should extend the full width
* to align with the scrollbar of the body
*/
if (!$(this).closest('.fht-tbody').length && $(this).is(':last-child')
&& !$(this).closest('.fht-fixed-column').length
) {
var padding = Math.max((($(this).innerWidth() - $(this).width()) / 2), settings.scrollbarOffset);
$(this).css({
'padding-right': parseInt($(this).css('padding-right')) + padding + 'px'
});
}
}
}
}
);
},

/*
* return object
* Iterates over cells within given set of rows
* Using:
* _trCellIterator(
* $('#table tr'),
* {
* beforeRow: function(context){
* context.rowCount++;
* //return true; // breaks row iteration
* },
* cell: function(context){
* context.cellCount += context.colspan;
* //return true; // breaks current row cell iteration
* },
* afterRow: function(context){
* if (context.rowCount > 2) {
* return true; // breaks row iteration
* }
* },
* },
* {
* rowCount: 0,
* cellCount: 0
* }
* );
*/
_trCellIterator: function($trs, callbacks, context) {
var rowspanData = {};

if (typeof callbacks === "undefined") {
callbacks = {};
}
if (typeof context === "undefined") {
context = {};
}

$trs.each(function(rowIndex) {
var colspanShift = 0,
rowspanShift = 0,
ret;

context.rowIndex = rowIndex;

if (typeof callbacks.beforeRow === 'function') {
if (ret = callbacks.beforeRow.call(this, context)) {
return false; // break
}
}

$(this).children().each(function(colIndex) {
var colspan = parseInt($(this).attr('colspan')) || 1,
rowspan = parseInt($(this).attr('rowspan')) || 1;

while (typeof rowspanData[colIndex + rowspanShift] !== "undefined"
&& rowspanData[colIndex + rowspanShift].rowspan > 1
) {
rowspanData[colIndex + rowspanShift].rowspan--;
rowspanShift += rowspanData[colIndex].colspan;
}

colIndex += (colspanShift + rowspanShift);

colspanShift += colspan - 1;
if (rowspan > 1) {
rowspanData[colIndex] = {
rowspan: rowspan,
colspan: colspan
};
}

context.colIndex = colIndex;
context.colspan = colspan;
context.rowspan = rowspan;

if (typeof callbacks.cell === 'function') {
if (ret = callbacks.cell.call(this, context)) {
return false; // break
}
}
});

/*
* Fixed Header and Footer should extend the full width
* to align with the scrollbar of the body
*/
if (!$(this).closest('.fht-tbody').length && $(this).is(':last-child') && !$(this).closest('.fht-fixed-column').length) {
var padding = Math.max((($(this).innerWidth() - $(this).width()) / 2), settings.scrollbarOffset);
$(this).css({
'padding-right': parseInt($(this).css('padding-right')) + padding + 'px'
});
if (typeof callbacks.afterRow === 'function') {
if (ret = callbacks.afterRow.call(this, context)) {
return false; // break
}
}
});

return context;
},

/**
* return int
* Calculates columns count within given set of rows
*/
_getColumnsCount: function($trs) {
var ret = helpers._trCellIterator(
$trs,
{
cell: function(context) {
context.count += context.colspan;
},
afterRow: function(context) {
if (context.count > 1 || context.rowspan == 1) {
return true;
}
}
},
{
count: 0
}
);

return ret.count;
},

/*
* return object
* Calculates columns width within given set of rows
*/
_getColumnsWidth: function($trs) {
var ret = helpers._trCellIterator(
$trs,
{
cell: function(context) {
if (typeof context.result[context.colIndex] === 'undefined'
|| context.result[context.colIndex].colspan > 1) {
var border = parseFloat($(this).outerWidth() - $(this).innerWidth())
/ (settings.borderCollapse ? 2 : 1);
context.result[context.colIndex] = {
width: $(this).width() + border,
colspan: context.colspan
};

if (context.colspan == 1) {
context.definedWidthCount++;
}
}
},
afterRow: function(context) {
if (context.definedWidthCount == context.columnsCount) {
return true; // break
}
}
},
{
result: {},
definedWidthCount: 0,
columnsCount: helpers._getColumnsCount($trs)
}
);

return ret.result;
},

/*
Expand Down
Loading