Declarative tables
This commit is contained in:
@ -1,12 +1,11 @@
|
||||
// http://officeopenxml.com/WPtableGrid.php
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
import { TableGrid } from "./grid";
|
||||
import { TableCell, WidthType } from "./table-cell";
|
||||
import { TableColumn } from "./table-column";
|
||||
import { WidthType } from "./table-cell";
|
||||
import { ITableFloatOptions, TableProperties } from "./table-properties";
|
||||
import { TableLayoutType } from "./table-properties/table-layout";
|
||||
import { TableRow } from "./table-row";
|
||||
|
||||
/*
|
||||
0-width columns don't get rendered correctly, so we need
|
||||
to give them some value. A reasonable default would be
|
||||
@ -18,8 +17,7 @@ import { TableRow } from "./table-row";
|
||||
algorithm will expand columns to fit its content
|
||||
*/
|
||||
export interface ITableOptions {
|
||||
readonly rows: number;
|
||||
readonly columns: number;
|
||||
readonly rows: TableRow[];
|
||||
readonly width?: number;
|
||||
readonly widthUnitType?: WidthType;
|
||||
readonly columnWidths?: number[];
|
||||
@ -36,14 +34,12 @@ export interface ITableOptions {
|
||||
|
||||
export class Table extends XmlComponent {
|
||||
private readonly properties: TableProperties;
|
||||
private readonly rows: TableRow[];
|
||||
|
||||
constructor({
|
||||
rows,
|
||||
columns,
|
||||
width = 100,
|
||||
widthUnitType = WidthType.AUTO,
|
||||
columnWidths = Array<number>(columns).fill(100),
|
||||
columnWidths = Array<number>(Math.max(...rows.map((row) => row.CellCount))).fill(100),
|
||||
margins: { marginUnitType, top, bottom, right, left } = { marginUnitType: WidthType.AUTO, top: 0, bottom: 0, right: 0, left: 0 },
|
||||
float,
|
||||
layout,
|
||||
@ -57,21 +53,12 @@ export class Table extends XmlComponent {
|
||||
this.properties.CellMargin.addTopMargin(top || 0, marginUnitType);
|
||||
this.properties.CellMargin.addLeftMargin(left || 0, marginUnitType);
|
||||
this.properties.CellMargin.addRightMargin(right || 0, marginUnitType);
|
||||
const grid = new TableGrid(columnWidths);
|
||||
|
||||
this.root.push(grid);
|
||||
this.root.push(new TableGrid(columnWidths));
|
||||
|
||||
this.rows = Array(rows)
|
||||
.fill(0)
|
||||
.map(() => {
|
||||
const cells = Array(columns)
|
||||
.fill(0)
|
||||
.map(() => new TableCell());
|
||||
const row = new TableRow(cells);
|
||||
return row;
|
||||
});
|
||||
|
||||
this.rows.forEach((x) => this.root.push(x));
|
||||
for (const row of rows) {
|
||||
this.root.push(row);
|
||||
}
|
||||
|
||||
if (float) {
|
||||
this.properties.setTableFloatProperties(float);
|
||||
@ -81,24 +68,4 @@ export class Table extends XmlComponent {
|
||||
this.properties.setLayout(layout);
|
||||
}
|
||||
}
|
||||
|
||||
public getRow(index: number): TableRow {
|
||||
const row = this.rows[index];
|
||||
|
||||
if (!row) {
|
||||
throw Error("Index out of bounds when trying to get row on table");
|
||||
}
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
public getColumn(index: number): TableColumn {
|
||||
// This is a convinence method for people who like to work with columns
|
||||
const cells = this.rows.map((row) => row.getCell(index));
|
||||
return new TableColumn(cells);
|
||||
}
|
||||
|
||||
public getCell(row: number, col: number): TableCell {
|
||||
return this.getRow(row).getCell(col);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user