Skip to content

Commit 665056b

Browse files
author
hhurz
committed
fixed rowspan/colspan issue #164 and kayalshri#93
1 parent c9b1bc3 commit 665056b

File tree

3 files changed

+176
-158
lines changed

3 files changed

+176
-158
lines changed

tableExport.js

Lines changed: 61 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -805,71 +805,59 @@
805805
var body = [];
806806
rowIndex = 0;
807807

808-
$hrows = $(this).find('thead').first().find(defaults.theadSelector);
809-
$hrows.each(function () {
810-
var h = [];
808+
var CollectPdfmakeData = function ($rows, colselector, length) {
809+
var rlength = 0;
811810

812-
ForEachVisibleCell(this, 'th,td', rowIndex, $hrows.length,
813-
function (cell, row, col) {
814-
if (typeof cell !== 'undefined' && cell !== null) {
811+
$rows.each(function () {
812+
var r = [];
813+
814+
ForEachVisibleCell(this, colselector, rowIndex, length,
815+
function (cell, row, col) {
816+
if (typeof cell !== 'undefined' && cell !== null) {
815817

816-
var colspan = parseInt(cell.getAttribute('colspan'));
817-
var rowspan = parseInt(cell.getAttribute('rowspan'));
818+
var colspan = parseInt(cell.getAttribute('colspan'));
819+
var rowspan = parseInt(cell.getAttribute('rowspan'));
818820

819-
var cellValue = parseString(cell, row, col);
821+
var cellValue = parseString(cell, row, col) || " ";
820822

821-
if (colspan > 1 || rowspan > 1) {
822-
colspan = colspan || 1;
823-
rowspan = rowspan || 1;
824-
h.push({colSpan: colspan, rowSpan: rowspan, text: cellValue});
823+
if (colspan > 1 || rowspan > 1) {
824+
colspan = colspan || 1;
825+
rowspan = rowspan || 1;
826+
r.push({colSpan: colspan, rowSpan: rowspan, text: cellValue});
827+
}
828+
else
829+
r.push(cellValue);
825830
}
826831
else
827-
h.push(cellValue);
828-
}
829-
});
832+
r.push(" ");
833+
});
830834

831-
if (h.length)
832-
body.push(h);
835+
if (r.length)
836+
body.push(r);
833837

834-
for(var i = widths.length; i < h.length;i++)
835-
widths.push("*");
838+
if ( rlength < r.length )
839+
rlength = r.length;
836840

837-
rowIndex++;
838-
});
841+
rowIndex++;
842+
});
843+
844+
return rlength;
845+
}
846+
847+
$hrows = $(this).find('thead').first().find(defaults.theadSelector);
848+
849+
var colcount = CollectPdfmakeData ($hrows, 'th,td', $hrows.length);
850+
851+
for(var i = widths.length; i < colcount;i++)
852+
widths.push("*");
839853

840854
$(this).find('tbody').each(function() {
841855
$rows.push.apply ($rows, $(this).find(defaults.tbodySelector));
842856
});
843857
if (defaults.tfootSelector.length)
844858
$rows.push.apply ($rows, $(this).find('tfoot').find(defaults.tfootSelector));
845859

846-
$($rows).each(function () {
847-
var r = [];
848-
849-
ForEachVisibleCell(this, 'td,th', rowIndex, $hrows.length + $rows.length,
850-
function (cell, row, col) {
851-
if (typeof cell !== 'undefined' && cell !== null) {
852-
var colspan = parseInt(cell.getAttribute('colspan'));
853-
var rowspan = parseInt(cell.getAttribute('rowspan'));
854-
855-
var cellValue = parseString(cell, row, col) || " ";
856-
857-
if (colspan > 1 || rowspan > 1) {
858-
colspan = colspan || 1;
859-
rowspan = rowspan || 1;
860-
r.push({colSpan: colspan, rowSpan: rowspan, text: cellValue});
861-
}
862-
else
863-
r.push(cellValue);
864-
}
865-
else
866-
r.push(" ");
867-
});
868-
869-
if (r.length)
870-
body.push(r);
871-
rowIndex++;
872-
});
860+
CollectPdfmakeData ($rows, 'th,td', $hrows.length + $rows.length);
873861

874862
var docDefinition = { content: [ {
875863
table: {
@@ -1323,47 +1311,52 @@
13231311
}).find(selector);
13241312

13251313
var rowColspan = 0;
1326-
var rowColIndex = 0;
13271314

13281315
$row.each(function (colIndex) {
13291316
if ($(this).data("tableexport-display") == 'always' ||
13301317
($(this).css('display') != 'none' &&
13311318
$(this).css('visibility') != 'hidden' &&
13321319
$(this).data("tableexport-display") != 'none')) {
1333-
if (isColumnIgnored($row, colIndex) === false) {
1334-
if (typeof (cellcallback) === "function") {
1335-
var c, Colspan = 0;
1336-
var r, Rowspan = 0;
1337-
1338-
// handle rowspans from previous rows
1339-
if (typeof rowspans[rowIndex] != 'undefined' && rowspans[rowIndex].length > 0) {
1340-
for (c = 0; c <= colIndex; c++) {
1341-
if (typeof rowspans[rowIndex][c] != 'undefined') {
1342-
cellcallback(null, rowIndex, c);
1343-
delete rowspans[rowIndex][c];
1344-
colIndex++;
1345-
}
1320+
if (typeof (cellcallback) === "function") {
1321+
var c, Colspan = 1;
1322+
var r, Rowspan = 1;
1323+
1324+
// handle rowspans from previous rows
1325+
if (typeof rowspans[rowIndex] != 'undefined' && rowspans[rowIndex].length > 0) {
1326+
var colCount = colIndex;
1327+
for (c = 0; c <= colCount; c++) {
1328+
if (typeof rowspans[rowIndex][c] != 'undefined') {
1329+
cellcallback(null, rowIndex, c);
1330+
delete rowspans[rowIndex][c];
1331+
colCount++;
13461332
}
13471333
}
1348-
rowColIndex = colIndex;
1334+
colIndex += rowspans[rowIndex].length;
1335+
}
13491336

1337+
if (isColumnIgnored($row, colIndex + rowColspan) === false) {
13501338
if ($(this).is("[colspan]")) {
1351-
Colspan = parseInt($(this).attr('colspan'));
1339+
Colspan = parseInt($(this).attr('colspan')) || 1;
1340+
1341+
for (c = 1; c < Colspan; c++)
1342+
if (isColumnIgnored($row, colIndex + c) === true)
1343+
Colspan--;
1344+
13521345
rowColspan += Colspan > 0 ? Colspan - 1 : 0;
13531346
}
13541347

13551348
if ($(this).is("[rowspan]"))
1356-
Rowspan = parseInt($(this).attr('rowspan'));
1349+
Rowspan = parseInt($(this).attr('rowspan')) || 1;
13571350

13581351
// output content of current cell
13591352
cellcallback(this, rowIndex, colIndex);
13601353

13611354
// handle colspan of current cell
1362-
for (c = 0; c < Colspan - 1; c++)
1355+
for (c = 1; c < Colspan; c++)
13631356
cellcallback(null, rowIndex, colIndex + c);
13641357

13651358
// store rowspan for following rows
1366-
if (Rowspan) {
1359+
if (Rowspan > 1) {
13671360
for (r = 1; r < Rowspan; r++) {
13681361
if (typeof rowspans[rowIndex + r] == 'undefined')
13691362
rowspans[rowIndex + r] = [];

0 commit comments

Comments
 (0)