-
Notifications
You must be signed in to change notification settings - Fork 31
/
StaticDataGridModel.ts
46 lines (36 loc) · 1.16 KB
/
StaticDataGridModel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import GridModel from './GridModel';
/**
* A simple model that displays static data
*/
class StaticDataGridModel extends GridModel {
private data: unknown[][];
private columnHeaders?: string[];
private numberOfColumns: number;
/**
* Create a model using the static data provided
* @param data Row data to display. First dimension is the number of rows, second dimension is the value for each column. Row/column count is derived from the data, and it is stringified for display.
* @param columnHeaders Optional names for the column headers
*/
constructor(data: unknown[][], columnHeaders?: string[]) {
super();
this.data = data;
this.numberOfColumns = Math.max(
...data.map(row => row.length),
columnHeaders?.length ?? 0
);
this.columnHeaders = columnHeaders;
}
get rowCount(): number {
return this.data.length;
}
get columnCount(): number {
return this.numberOfColumns;
}
textForCell(column: number, row: number): string {
return `${this.data[row]?.[column]}`;
}
textForColumnHeader(column: number): string {
return this.columnHeaders?.[column] ?? '';
}
}
export default StaticDataGridModel;