AnthoLume/ngtemplates/components/table.templ

50 lines
1.1 KiB
Plaintext
Raw Permalink Normal View History

2024-10-06 21:01:37 +00:00
package components
type TableCellFormatter[T any] func(data T) templ.Component
type TableColumn[T any] struct {
Name string // Column Name
Getter func(T) string // Data Getter
Formatter TableCellFormatter[T] // Data Formatter
}
templ (c *TableColumn[T]) getCell(d T) {
if c.Formatter != nil {
@c.Formatter(d)
} else if c.Getter != nil {
{ c.Getter(d) }
} else {
"Unknown"
}
}
templ Table[T any](columns []TableColumn[T], rows []T) {
<table class="min-w-full leading-normal bg-white dark:bg-gray-700 text-sm">
<thead class="text-gray-800 dark:text-gray-400">
<tr>
for _, column := range columns {
<th class="p-3 font-normal text-left uppercase border-b border-gray-200 dark:border-gray-800">
{ column.Name }
</th>
}
</tr>
</thead>
<tbody class="text-black dark:text-white">
if len(rows) == 0 {
<tr>
<td class="text-center p-3" colspan="4">No Results</td>
</tr>
}
for _, row := range rows {
<tr>
for _, column := range columns {
<td class="p-3 border-b border-gray-200">
@column.getCell(row)
</td>
}
</tr>
}
</tbody>
</table>
}