mirror of
https://github.com/dolanmiu/docx.git
synced 2026-11-30 16:47:55 +01:00
docs: explain that only header rows at the top of a table repeat (#3745)
Word repeats a table's first row and the rows straight after it that set tableHeader, and ignores tableHeader on rows further down. Document this, and show how to give each part of a long table its own repeating header by making each part a table of its own. Closes #2425 Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -239,6 +239,35 @@ const doc = new Document({
|
||||
});
|
||||
```
|
||||
|
||||
Only rows at the top of a table repeat. Word repeats the table's first row, and the rows straight after it that also set `tableHeader`, and ignores `tableHeader` on any row further down. So a table has one set of header rows, and a row with `tableHeader` in the middle of it neither repeats nor stops the header rows above it from repeating.
|
||||
|
||||
To give each part of a long table a header of its own, make each part a table, with its header row first. Word joins tables that are next to each other into one, so put a paragraph between them, such as a heading for the part:
|
||||
|
||||
```ts live
|
||||
import { Document, HeadingLevel, Paragraph, Table, TableCell, TableRow } from "docx";
|
||||
|
||||
const cell = (text: string) => new TableCell({ children: [new Paragraph(text)] });
|
||||
|
||||
const part = (name: string) => [
|
||||
new Paragraph({ text: name, heading: HeadingLevel.HEADING_2 }),
|
||||
new Table({
|
||||
rows: [
|
||||
// The first row of each table repeats on each page that table runs onto
|
||||
new TableRow({ children: [cell(`${name} item`), cell("Quantity")], tableHeader: true }),
|
||||
...Array.from({ length: 40 }, (_, index) => new TableRow({ children: [cell(`${name} ${index + 1}`), cell(`${index + 1}`)] })),
|
||||
],
|
||||
}),
|
||||
];
|
||||
|
||||
const doc = new Document({
|
||||
sections: [
|
||||
{
|
||||
children: [...part("Fruit"), ...part("Vegetable"), ...part("Grain")],
|
||||
},
|
||||
],
|
||||
});
|
||||
```
|
||||
|
||||
### Pagination
|
||||
|
||||
#### Prevent row pagination
|
||||
|
||||
@@ -59,7 +59,12 @@ import { type ITableCellSpacingProperties, createTableCellSpacing } from "../tab
|
||||
export type ITableRowPropertiesOptionsBase = {
|
||||
/** Whether the row can be split across pages (cantSplit) */
|
||||
readonly cantSplit?: boolean;
|
||||
/** Whether the row should be repeated as a header row on each page (tblHeader) */
|
||||
/**
|
||||
* Whether the row should be repeated as a header row on each page (tblHeader).
|
||||
*
|
||||
* Only the table's first row and the rows straight after it that also set this repeat. Word ignores it on
|
||||
* any row further down, so to repeat a different header for each part of a table, make each part a table of its own.
|
||||
*/
|
||||
readonly tableHeader?: boolean;
|
||||
/** Row height configuration (trHeight) */
|
||||
readonly height?: {
|
||||
|
||||
Reference in New Issue
Block a user