|
1 | | -import { Filter, ReportHeader, TableColumn, TableHeaderCell } from './reportInterfaces'; |
| 1 | +import { Filter, HeaderColumn, ReportHeader, TableColumn, TableHeaderCell } from './reportInterfaces'; |
2 | 2 |
|
3 | 3 | export function generateHtmlTable<T>( |
4 | | - headerRows: TableHeaderCell[][], |
| 4 | + headerRows: HeaderColumn[], |
5 | 5 | columns: Array<TableColumn<T>>, |
6 | 6 | rows: T[], |
7 | 7 | reportHeader: ReportHeader[], |
8 | 8 | filters: Filter[] = [], |
9 | 9 | tableClass = 'slds-table slds-table_cell-buffer slds-table_bordered slds-table_striped slds-table_col-bordered', |
10 | 10 | ariaLabel = '' |
11 | 11 | ): string { |
| 12 | + const transformedHeader: TableHeaderCell[][] = transform(headerRows); |
| 13 | + |
12 | 14 | const thead = ` |
13 | 15 | <thead> |
14 | | - ${headerRows |
| 16 | + ${transformedHeader |
15 | 17 | .map( |
16 | 18 | (row) => ` |
17 | 19 | <tr> |
18 | 20 | ${row |
19 | | - .map( |
20 | | - (cell) => ` |
21 | | - <th |
22 | | - ${cell.colspan ? `colspan="${cell.colspan}"` : ''} |
23 | | - ${cell.rowspan ? `rowspan="${cell.rowspan}"` : ''} |
24 | | - style="width:${cell?.width || 'auto'}" |
25 | | - > |
26 | | - <div class="filter-header"> |
27 | | - <span class="filter-label">${cell.label}</span> |
28 | | - </div> |
29 | | - </th> |
30 | | - ` |
31 | | - ) |
| 21 | + .map((cell: TableHeaderCell) => { |
| 22 | + const colspanAttr = cell.colspan ? `colspan="${cell.colspan}"` : ''; |
| 23 | + const rowspanAttr = cell.rowspan ? `rowspan="${cell.rowspan}"` : ''; |
| 24 | + const styleAttr = `style="${cell.styles ?? 'width:auto;'}"`; |
| 25 | + return ` |
| 26 | + <th ${colspanAttr} ${rowspanAttr} ${styleAttr}> |
| 27 | + <div class="filter-header"> |
| 28 | + <span class="filter-label">${cell.label}</span> |
| 29 | + </div> |
| 30 | + </th> |
| 31 | + `; |
| 32 | + }) |
32 | 33 | .join('')} |
33 | 34 | </tr> |
34 | 35 | ` |
@@ -58,7 +59,7 @@ export function generateHtmlTable<T>( |
58 | 59 | oninput="filterAndSearchTable()" |
59 | 60 | /> |
60 | 61 | </div> |
61 | | -
|
| 62 | + |
62 | 63 | <div class="filter-toggle-button" onclick="toggleFilterDropdown()"> |
63 | 64 | Filters |
64 | 65 | <svg id="chevron-down" class="chevron-icon" xmlns="http://www.w3.org/2000/svg" width="10" height="10" fill="currentColor" viewBox="0 0 16 16"> |
@@ -118,8 +119,9 @@ export function generateHtmlTable<T>( |
118 | 119 | const title: string = col.title ? col.title(row) : ''; |
119 | 120 | const value: string = col.filterValue(row).toString(); |
120 | 121 | const cellContent: string = col.cell(row); |
| 122 | + const style: string = col.styles ? col.styles(row) : ''; |
121 | 123 | const dataAttr: string = ['name', 'oldName'].includes(key) ? `data-name="${value.toLowerCase()}"` : ''; |
122 | | - return `<td ${dataAttr} title="${title}" key="${key}" value="${value}">${cellContent}</td>`; |
| 124 | + return `<td ${dataAttr} title="${title}" key="${key}" value="${value}" style="${style}">${cellContent}</td>`; |
123 | 125 | }) |
124 | 126 | .join('')} |
125 | 127 | </tr> |
@@ -172,3 +174,35 @@ export function generateHtmlTable<T>( |
172 | 174 | </div> |
173 | 175 | `; |
174 | 176 | } |
| 177 | + |
| 178 | +function transform(columnInput): TableHeaderCell[][] { |
| 179 | + const row1 = []; |
| 180 | + const row2 = []; |
| 181 | + |
| 182 | + columnInput.forEach((item) => { |
| 183 | + if (item.subColumn && item.subColumn.length > 0) { |
| 184 | + row1.push({ |
| 185 | + label: item.label, |
| 186 | + colspan: item.subColumn.length, |
| 187 | + }); |
| 188 | + |
| 189 | + item.subColumn.forEach((sub) => { |
| 190 | + row2.push({ |
| 191 | + label: sub.label, |
| 192 | + key: sub.key, |
| 193 | + }); |
| 194 | + }); |
| 195 | + } else { |
| 196 | + const row1Entry: TableHeaderCell = { |
| 197 | + label: item.label, |
| 198 | + }; |
| 199 | + |
| 200 | + if (item.rowspan) row1Entry.rowspan = Number(item.rowspan); |
| 201 | + if (item.key) row1Entry.key = item.key; |
| 202 | + |
| 203 | + row1.push(row1Entry); |
| 204 | + } |
| 205 | + }); |
| 206 | + |
| 207 | + return [row1, row2] as unknown as TableHeaderCell[][]; |
| 208 | +} |
0 commit comments