Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Table alignment #2159

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
46 changes: 39 additions & 7 deletions examples/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,38 @@ var docDefinition = {
]
}
},
{ text: 'Table alignment', pageBreak: 'before', style: 'subheader' },
'via tableAlignment property',
{
style: 'tableExample',
tableAlignment: 'left',
table: {
body: [
['Column 1', 'Column 2', 'Column 3'],
['One value goes here', 'Another one here', 'OK?']
]
}
},
{
style: 'tableExample',
tableAlignment: 'center',
table: {
body: [
['Column 1', 'Column 2', 'Column 3'],
['One value goes here', 'Another one here', 'OK?']
]
}
},
{
style: 'tableExample',
tableAlignment: 'right',
table: {
body: [
['Column 1', 'Column 2', 'Column 3'],
['One value goes here', 'Another one here', 'OK?']
]
}
},
{ text: 'Headers', pageBreak: 'before', style: 'subheader' },
'You can declare how many rows should be treated as a header. Headers are automatically repeated on the following pages',
{ text: ['It is also possible to set keepWithHeaderRows to make sure there will be no page-break between the header and these rows. Take a look at the document-definition and play with it. If you set it to one, the following table will automatically start on the next page, since there\'s not enough space for the first row to be rendered here'], color: 'gray', italics: true },
Expand Down Expand Up @@ -300,9 +332,9 @@ var docDefinition = {
body: [
['Sample value 1', 'Sample value 2', 'Sample value 3'],
[
{text: 'Sample value 1',fillOpacity:0.15,fillColor:'blue'},
{text: 'Sample value 2',fillOpacity:0.60,fillColor:'blue'},
{text: 'Sample value 3',fillOpacity:0.85,fillColor:'blue'},
{ text: 'Sample value 1', fillOpacity: 0.15, fillColor: 'blue' },
{ text: 'Sample value 2', fillOpacity: 0.60, fillColor: 'blue' },
{ text: 'Sample value 3', fillOpacity: 0.85, fillColor: 'blue' },
],
[
{text: 'Sample value 1', fillOpacity: 0.15, fillColor: ['stripe45d', 'blue']},
Expand All @@ -325,9 +357,9 @@ var docDefinition = {
body: [
['Sample value 1', 'Sample value 2', 'Sample value 3'],
[
{text: 'Sample value 1',fillOpacity:0.15},
{text: 'Sample value 2',fillOpacity:0.60},
{text: 'Sample value 3',fillOpacity:0.85},
{ text: 'Sample value 1', fillOpacity: 0.15 },
{ text: 'Sample value 2', fillOpacity: 0.60 },
{ text: 'Sample value 3', fillOpacity: 0.85 },
],
['Sample value 1', 'Sample value 2', 'Sample value 3']
]
Expand All @@ -351,7 +383,7 @@ var docDefinition = {
layout: {
fillColor: 'blue',
fillOpacity: function (rowIndex, node, columnIndex) {
return (rowIndex/8+columnIndex/3);
return (rowIndex / 8 + columnIndex / 3);
}
}
},
Expand Down
24 changes: 24 additions & 0 deletions src/DocPreprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,30 @@ class DocPreprocessor {
}
}

// table alignment on page
switch (node.tableAlignment) {
case 'right':
node.width = 'auto';
node = {
columns: [
{ width: '*', text: '' },
node
]
};
break;

case 'center':
node.width = 'auto';
node = {
columns: [
{ width: '*', text: '' },
node,
{ width: '*', text: '' }
]
};
break;
}

return node;
}

Expand Down
47 changes: 47 additions & 0 deletions tests/integration/tables.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,51 @@ describe('Integration test: tables', function () {
assert.deepEqual(getColumnText(lines, { cell: 1 }), 'Row 2');
});


it('renders a simple table with right alignment', function () {
var dd = {
content:
{
tableAlignment: 'right',
table: {
body: [
['Column 1', 'Column 2'],
['Value 1', 'Value 2']
]
},
}
};

var pages = testHelper.renderPages('A6', dd);
var lines = getCells(pages, { pageNumber: 0 });

const availableWidth = sizes.A6[0] - testHelper.MARGINS.right;
const tableWidth = TABLE_PADDING_X * 3 + TABLE_BORDER_STRENGTH * 2 + lines[0].item.maxWidth * 2;
const startXAligned = availableWidth - tableWidth;

assert.equal(lines[0].item.x, startXAligned);
});

it('renders a simple table with center alignment', function () {
var dd = {
content: {
tableAlignment: 'center',
table: {
body: [
['Column 1', 'Column 2'],
['Value 1', 'Value 2']
]
}
}
};

var pages = testHelper.renderPages('A6', dd);
var lines = getCells(pages, { pageNumber: 0 });

const tableWidth = TABLE_PADDING_X * 2 + TABLE_BORDER_STRENGTH + lines[0].item.maxWidth * 2;
const startXAligned = (sizes.A6[0] - tableWidth) / 2;

assert.equal(lines[0].item.x, startXAligned);
});

});
Loading