From 561e7d073e85d5ecab66d90c998906b53f3fe322 Mon Sep 17 00:00:00 2001 From: Libor M Date: Wed, 30 Dec 2020 13:51:23 +0100 Subject: [PATCH] table alignment #72 --- examples/tables.js | 46 ++++++++++++++++++++++++++----- src/DocPreprocessor.js | 24 ++++++++++++++++ tests/integration/tables.spec.js | 47 ++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 7 deletions(-) diff --git a/examples/tables.js b/examples/tables.js index 3e9dc3332..49b520357 100644 --- a/examples/tables.js +++ b/examples/tables.js @@ -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 }, @@ -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' }, ], ['Sample value 1', 'Sample value 2', 'Sample value 3'] ] @@ -315,9 +347,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'] ] @@ -341,7 +373,7 @@ var docDefinition = { layout: { fillColor: 'blue', fillOpacity: function (rowIndex, node, columnIndex) { - return (rowIndex/8+columnIndex/3); + return (rowIndex / 8 + columnIndex / 3); } } }, diff --git a/src/DocPreprocessor.js b/src/DocPreprocessor.js index 1b09c5f16..0755141c1 100644 --- a/src/DocPreprocessor.js +++ b/src/DocPreprocessor.js @@ -113,6 +113,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; } diff --git a/tests/integration/tables.spec.js b/tests/integration/tables.spec.js index 7a2553af3..62f2988d4 100644 --- a/tests/integration/tables.spec.js +++ b/tests/integration/tables.spec.js @@ -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); + }); + });