(props: { rows: Record<string, unknown>[] })
| 54 | } |
| 55 | |
| 56 | function DataTable(props: { rows: Record<string, unknown>[] }) { |
| 57 | const columns = () => { |
| 58 | const cols = new Set<string>() |
| 59 | for (const row of props.rows) { |
| 60 | for (const key of Object.keys(row)) cols.add(key) |
| 61 | } |
| 62 | return [...cols] |
| 63 | } |
| 64 | |
| 65 | return ( |
| 66 | <Show when={props.rows.length > 0} fallback={<div data-empty>(no data)</div>}> |
| 67 | <table> |
| 68 | <thead> |
| 69 | <tr> |
| 70 | <For each={columns()}>{(col) => <th>{col}</th>}</For> |
| 71 | </tr> |
| 72 | </thead> |
| 73 | <tbody> |
| 74 | <For each={props.rows}> |
| 75 | {(row) => ( |
| 76 | <tr> |
| 77 | <For each={columns()}>{(col) => <td>{renderCell(row[col])}</td>}</For> |
| 78 | </tr> |
| 79 | )} |
| 80 | </For> |
| 81 | </tbody> |
| 82 | </table> |
| 83 | </Show> |
| 84 | ) |
| 85 | } |
| 86 | |
| 87 | function renderCell(value: unknown) { |
| 88 | if (value === null || value === undefined) return "" |
nothing calls this directly
no test coverage detected