Update htmlTable.ts

This commit is contained in:
freearhey 2025-01-24 20:53:09 +03:00
parent e17ce1c403
commit eef2bf2815

View file

@ -2,9 +2,15 @@ type Column = {
name: string name: string
nowrap?: boolean nowrap?: boolean
align?: string align?: string
colspan?: number
} }
type DataItem = string[] type DataItem = {
value: string
nowrap?: boolean
align?: string
colspan?: number
}[]
export class HTMLTable { export class HTMLTable {
data: DataItem[] data: DataItem[]
@ -20,20 +26,23 @@ export class HTMLTable {
output += ' <thead>\r\n <tr>' output += ' <thead>\r\n <tr>'
for (const column of this.columns) { for (const column of this.columns) {
output += `<th align="left">${column.name}</th>` const nowrap = column.nowrap ? ' nowrap' : ''
const align = column.align ? ` align="${column.align}"` : ''
const colspan = column.colspan ? ` colspan="${column.colspan}"` : ''
output += `<th${align}${nowrap}${colspan}>${column.name}</th>`
} }
output += '</tr>\r\n </thead>\r\n' output += '</tr>\r\n </thead>\r\n'
output += ' <tbody>\r\n' output += ' <tbody>\r\n'
for (const item of this.data) { for (const row of this.data) {
output += ' <tr>' output += ' <tr>'
let i = 0 for (const item of row) {
for (const prop in item) { const nowrap = item.nowrap ? ' nowrap' : ''
const column = this.columns[i] const align = item.align ? ` align="${item.align}"` : ''
const nowrap = column.nowrap ? ' nowrap' : '' const colspan = item.colspan ? ` colspan="${item.colspan}"` : ''
const align = column.align ? ` align="${column.align}"` : ''
output += `<td${align}${nowrap}>${item[prop]}</td>` output += `<td${align}${nowrap}${colspan}>${item.value}</td>`
i++
} }
output += '</tr>\r\n' output += '</tr>\r\n'
} }