-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-table.component.ts
More file actions
94 lines (91 loc) · 3.06 KB
/
data-table.component.ts
File metadata and controls
94 lines (91 loc) · 3.06 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { TableModule } from 'primeng/table';
import { PrimeTemplate } from 'primeng/api';
export interface DataTableColumn {
field?: string;
header?: string;
sortable?: boolean;
style?: string;
headerStyle?: string;
selectionMode?: 'single' | 'multiple';
}
@Component({
selector: 'data-table',
host: { style: 'display: block' },
standalone: true,
imports: [TableModule, PrimeTemplate],
template: `
<p-table
[value]="value"
[columns]="columns"
[stripedRows]="stripedRows"
[showGridlines]="showGridlines"
[loading]="loading"
[size]="size"
[scrollable]="scrollable"
[scrollHeight]="scrollHeight || undefined"
[paginator]="paginator"
[rows]="rows"
[rowsPerPageOptions]="rowsPerPageOptions.length ? rowsPerPageOptions : undefined"
[selectionMode]="selectionMode"
[selection]="selection"
(selectionChange)="selectionChange.emit($event)"
[dataKey]="dataKey"
>
<ng-template pTemplate="header" let-columns>
<tr>
@for (col of columns; track $index) {
@if (col.selectionMode === 'single') {
<th [style]="col.headerStyle || 'width: 3rem'"></th>
} @else if (col.selectionMode === 'multiple') {
<th [style]="col.headerStyle || 'width: 3rem'">
<p-tableHeaderCheckbox></p-tableHeaderCheckbox>
</th>
} @else {
<th [pSortableColumn]="col.sortable ? col.field : null" [style]="col.headerStyle || ''">
{{ col.header }}
@if (col.sortable) {
<p-sortIcon [field]="col.field"></p-sortIcon>
}
</th>
}
}
</tr>
</ng-template>
<ng-template pTemplate="body" let-rowData let-columns="columns">
<tr [pSelectableRow]="rowData">
@for (col of columns; track $index) {
@if (col.selectionMode === 'single') {
<td>
<p-tableRadioButton [value]="rowData"></p-tableRadioButton>
</td>
} @else if (col.selectionMode === 'multiple') {
<td>
<p-tableCheckbox [value]="rowData"></p-tableCheckbox>
</td>
} @else {
<td [style]="col.style || ''">{{ rowData[col.field] }}</td>
}
}
</tr>
</ng-template>
</p-table>
`,
})
export class DataTableComponent {
@Input() value: any[] = [];
@Input() columns: DataTableColumn[] = [];
@Input() stripedRows = false;
@Input() showGridlines = false;
@Input() loading = false;
@Input() size: 'small' | 'large' | undefined = undefined;
@Input() scrollable = false;
@Input() scrollHeight = '';
@Input() paginator = false;
@Input() rows = 5;
@Input() rowsPerPageOptions: number[] = [5, 10, 25];
@Input() selectionMode: 'single' | 'multiple' | undefined = undefined;
@Input() selection: any = null;
@Output() selectionChange = new EventEmitter<any>();
@Input() dataKey = 'id';
}