Skip to content

Commit

Permalink
Add support for repeating multiple columns using horizontalPageBreakR…
Browse files Browse the repository at this point in the history
…epeat option

Fixes #911
  • Loading branch information
ecosse3 authored and simonbengtsson committed Aug 23, 2023
1 parent 56a045e commit 6879859
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export interface UserOptions {
html?: string | HTMLTableElement
columns?: ColumnInput[]
horizontalPageBreak?: boolean
// column data key to repeat if horizontalPageBreak = true
horizontalPageBreakRepeat?: string | number
// Column data key to repeat if horizontalPageBreak = true
horizontalPageBreakRepeat?: string[] | number[] | string | number

// Styles
styles?: Partial<Styles>
Expand Down
2 changes: 1 addition & 1 deletion src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface Settings {
tableLineWidth: number
tableLineColor: Color
horizontalPageBreak?: boolean
horizontalPageBreakRepeat?: string | number | null
horizontalPageBreakRepeat?: string | number | string[] | number[] | null
}

export type StyleProp =
Expand Down
64 changes: 42 additions & 22 deletions src/tablePrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,37 @@ const getColumnsCanFitInPage = (
table: Table,
config: any = {}
): ColumnFitInPageResult => {
// get page width
// Get page width
const availablePageWidth = getPageAvailableWidth(doc, table)
let remainingWidth = availablePageWidth
// get column data key to repeat
const horizontalPageBreakRepeat = table.settings.horizontalPageBreakRepeat

// Get column data key to repeat
const repeatColumnsMap = new Map<number, Column>();
let repeatColumn = null
const cols: number[] = []
const columns: Column[] = []

const len = table.columns.length
let i = config && config.start ? config.start : 0
// code to repeat the given column in split pages
if (horizontalPageBreakRepeat != null) {
let i = config?.start ?? 0;

const horizontalPageBreakRepeat = table.settings.horizontalPageBreakRepeat
// Code to repeat the given column in split pages
if (horizontalPageBreakRepeat !== null && horizontalPageBreakRepeat !== undefined && Array.isArray(horizontalPageBreakRepeat)) {
for (const field of horizontalPageBreakRepeat) {
const col = table.columns.find(
(item) =>
item.dataKey === field ||
item.index === field
)
if (col) {
repeatColumnsMap.set(col.index, col);
cols.push(col.index)
columns.push(table.columns[col.index])
remainingWidth = remainingWidth - col.wrappedWidth
}
}
// It can be a single value of type string or number (even number: 0)
} else if (horizontalPageBreakRepeat !== null && horizontalPageBreakRepeat !== undefined) {
repeatColumn = table.columns.find(
(item) =>
item.dataKey === horizontalPageBreakRepeat ||
Expand All @@ -44,29 +63,30 @@ const getColumnsCanFitInPage = (
remainingWidth = remainingWidth - repeatColumn.wrappedWidth
}
}

while (i < len) {
if (repeatColumn?.index === i) {
i++ // prevent columnDataKeyToRepeat to be pushed twice in a page
continue
// Prevent columnDataKeyToRepeat from being pushed twice on a page
if ((Array.isArray(horizontalPageBreakRepeat) && repeatColumnsMap.get(i))
|| repeatColumn?.index === i) {
i++;
continue;
}
const colWidth = table.columns[i].wrappedWidth

const colWidth = table.columns[i].wrappedWidth;
if (remainingWidth < colWidth) {
// check if it's first column in the sequence then add it into result
if (i === 0 || i === config.start) {
// this cell width is more than page width set it available pagewidth
/* table.columns[i].wrappedWidth = availablePageWidth
table.columns[i].minWidth = availablePageWidth */
cols.push(i)
columns.push(table.columns[i])
cols.push(i);
columns.push(table.columns[i]);
}
// can't print more columns in same page
break
break;
}
cols.push(i)
columns.push(table.columns[i])
remainingWidth = remainingWidth - colWidth
i++

cols.push(i);
columns.push(table.columns[i]);
remainingWidth -= colWidth;
i++;
}

return { colIndexes: cols, columns, lastIndex: i }
}

Expand Down

0 comments on commit 6879859

Please sign in to comment.