diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2020-11-17 22:19:40 +0100 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2020-11-17 22:19:40 +0100 |
| commit | 6faf447ab9d6b15b4a4eb6aac716fc512c34cfe3 (patch) | |
| tree | 82d730b71792af2f5aa95e2b61d16b4f6d5a2887 /grid | |
| parent | 95ab7c19fff815de0007be3268e2fb00132980e7 (diff) | |
| download | web-components-6faf447ab9d6b15b4a4eb6aac716fc512c34cfe3.tar.xz web-components-6faf447ab9d6b15b4a4eb6aac716fc512c34cfe3.zip | |
cleanup cell creation
allow dataId and cellValue
cellValue takes precedence over dataId
Diffstat (limited to 'grid')
| -rw-r--r-- | grid/src/grid.ts | 34 |
1 files changed, 13 insertions, 21 deletions
diff --git a/grid/src/grid.ts b/grid/src/grid.ts index 3f59614..f263ac7 100644 --- a/grid/src/grid.ts +++ b/grid/src/grid.ts @@ -7,7 +7,7 @@ interface GridProps { columns: Array<GridColumn>; pageSize?: number; search?: SearchConfiguration; - tableClassName?: string; + tableClassName?: string } interface GridConfiguration { @@ -17,7 +17,7 @@ interface GridConfiguration { columns: Array<GridColumn>; pageSize: number; search: SearchConfiguration; - tableClassName: string; + tableClassName: string } interface SearchConfiguration { @@ -188,7 +188,7 @@ export default class Grid { } wrapper.innerHTML = ""; let items = data ?? this.configuration.data; - if (this.configuration.pageSize !== 0) { + if (this.configuration.pageSize > 0) { items = items.slice(0, this.configuration.pageSize); } for (const item of items) { @@ -197,31 +197,23 @@ export default class Grid { row.dataset.id = item[this.configuration.id]; for (const val of this.configuration.columns) { const cell = document.createElement("td"); + cell.className = "text-break"; if (val.width) cell.style.width = val.width; if (val.maxWidth) cell.style.maxWidth = val.maxWidth; if (val.minWidth) cell.style.minWidth = val.minWidth; - if (val.dataId !== undefined && val.dataId.length > 0) { - if (Array.isArray(val.dataId)) { - // @ts-ignore - val.cellValue = val.dataId; - this.log(val.dataId); - } else { - // @ts-ignore - val.cellValue = item[val.dataId]; - } - } - cell.className = "text-break"; - if (typeof val.cellValue === "string" || typeof val.cellValue === "number") { - cell.innerText = val.cellValue; - } else if (val.cellValue instanceof Element) { - cell.appendChild(val.cellValue); - } else if (val.cellValue instanceof Function) { + if (val.cellValue instanceof Function) { const computed = val.cellValue(item); if (computed instanceof Element) cell.appendChild(computed); else if (typeof computed === "string" || typeof computed === "number") cell.innerText = computed as string; - } else if (Array.isArray(val.cellValue)) { - cell.innerText = this.getNestedFieldValue(item, val.cellValue); + } else if (Array.isArray(val.dataId)) { + cell.innerText = this.getNestedFieldValue(item, val.dataId); + } else if (typeof val.dataId === "string" && val.dataId.length > 0) { + cell.innerText = item[val.dataId]; + } else if (typeof val.cellValue === "string" || typeof val.cellValue === "number") { + cell.innerText = val.cellValue; + } else if (val.cellValue instanceof Element) { + cell.appendChild(val.cellValue); } row.appendChild(cell); } |
