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

Feature/cell width #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,25 @@ table.onPageAdd(function (table, row, ev) {

## Changelogs

### Col span

You can customize the single cells width by defining the "columnSpan" property and defining an object instead of a plain text content.

```js
const tableBody = [
{
columnA: "firstRowFirstCell",
columnB: "firstRowSecondCell"
},
{
columnA: {
content:"secondRowUniqueCell",
colSpan: 2
}
}
]
```

### 0.4.0
Thank you, contributors!

Expand Down
43 changes: 35 additions & 8 deletions voilab-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,19 @@ var lodash = require('lodash'),
top: 0
},
data = row._renderedContent.data[column.id] || '',
colSpan = row._renderedContent.colSpan[column.id] || 1,
renderer = isHeader ? column.headerRenderer : column.renderer,
y = pos.y,
x = pos.x;

if(colSpan && colSpan > 1) {
const columnIndex = self.getColumns().indexOf(column);
const nextCells = self.getColumns().slice(columnIndex, columnIndex+colSpan);
width = nextCells.reduce((sum,col)=>{
return sum + col.width;
},0);
}

// Top and bottom padding (only if valign is not set)
if (!column.valign) {
if (isHeader) {
Expand Down Expand Up @@ -152,14 +161,31 @@ var lodash = require('lodash'),
pos.y = self.pdf.y || self.pdf.page.margins.top;
}

lodash.forEach(self.getColumns(), function (column) {
if ((!isHeader && column.fill) || (isHeader && column.headerFill)) {
addCellBackground(self, column, row, pos, index, isHeader);
// check if the column at the given index of the current row must be drawn or not (maybe because the previous cell has a colSpan > 1)
const skippedCell = (row, index) => {
if(index === 0){
return false;
}
if ((!isHeader && column.border) || (isHeader && column.headerBorder)) {
addCellBorder(self, column, row, pos, isHeader);
const previousCells = self.getColumns().slice(0, index).map(col=>row[col.id]);
let toSkip = false;
previousCells.forEach((cell,cellIndex) => {
if(cell && cell.colSpan && cell.colSpan + cellIndex > index){
toSkip = true;
}
})
return toSkip;
};

lodash.forEach(self.getColumns(), function (column, index) {
if(!skippedCell(row, index)){
if ((!isHeader && column.fill) || (isHeader && column.headerFill)) {
addCellBackground(self, column, row, pos, index, isHeader);
}
if ((!isHeader && column.border) || (isHeader && column.headerBorder)) {
addCellBorder(self, column, row, pos, isHeader);
}
addCell(self, column, row, pos, isHeader);
}
addCell(self, column, row, pos, isHeader);
});

self.pdf.y = pos.y + row._renderedContent.height;
Expand All @@ -168,7 +194,7 @@ var lodash = require('lodash'),
setRowHeight = function (self, row, isHeader) {
var max_height = 0;

row._renderedContent = {data: {}, dataHeight: {}, contentHeight: {}};
row._renderedContent = {data: {}, dataHeight: {}, contentHeight: {}, colSpan: {}};

lodash.forEach(self.getColumns(), function (column) {
var renderer = isHeader ? column.headerRenderer : column.renderer,
Expand All @@ -194,7 +220,8 @@ var lodash = require('lodash'),

// backup content so we don't need to call the renderer a second
// time when really rendering the column
row._renderedContent.data[column.id] = content;
row._renderedContent.data[column.id] = Object.isObject(content) ? content.content : content;
row._renderedContent.colSpan[column.id] = Object.isObject(content) ? content.colSpan : 1;
row._renderedContent.dataHeight[column.id] = height;

// check max row height
Expand Down