|
| 1 | +import { customElement } from 'lit/decorators.js' |
| 2 | +import { html, LitElement } from 'lit' |
| 3 | +import { repeat } from 'lit/directives/repeat.js' |
| 4 | +import { |
| 5 | + ColumnDef, |
| 6 | + flexRender, |
| 7 | + getCoreRowModel, |
| 8 | + getSortedRowModel, |
| 9 | + TableController, |
| 10 | +} from '@tanstack/lit-table' |
| 11 | +import config from '../twind.config' |
| 12 | +import { styleMap } from 'lit/directives/style-map.js' |
| 13 | +import { virtualize, virtualizerRef } from '@lit-labs/virtualizer/virtualize.js' |
| 14 | +import { makeData, Person } from './makeData.ts' |
| 15 | + |
| 16 | +const columns: ColumnDef<Person>[] = [ |
| 17 | + { |
| 18 | + accessorKey: 'id', |
| 19 | + header: 'ID', |
| 20 | + size: 60, |
| 21 | + }, |
| 22 | + { |
| 23 | + accessorKey: 'firstName', |
| 24 | + cell: info => info.getValue(), |
| 25 | + }, |
| 26 | + { |
| 27 | + accessorFn: row => row.lastName, |
| 28 | + id: 'lastName', |
| 29 | + cell: info => info.getValue(), |
| 30 | + header: () => html`<span>Last Name</span>`, |
| 31 | + }, |
| 32 | + { |
| 33 | + accessorKey: 'age', |
| 34 | + header: () => 'Age', |
| 35 | + size: 50, |
| 36 | + }, |
| 37 | + { |
| 38 | + accessorKey: 'visits', |
| 39 | + header: () => html`<span>Visits</span>`, |
| 40 | + size: 50, |
| 41 | + }, |
| 42 | + { |
| 43 | + accessorKey: 'status', |
| 44 | + header: 'Status', |
| 45 | + }, |
| 46 | + { |
| 47 | + accessorKey: 'progress', |
| 48 | + header: 'Profile Progress', |
| 49 | + size: 80, |
| 50 | + }, |
| 51 | + { |
| 52 | + accessorKey: 'createdAt', |
| 53 | + header: 'Created At', |
| 54 | + cell: info => info.getValue<Date>().toLocaleString(), |
| 55 | + size: 250, |
| 56 | + }, |
| 57 | +] |
| 58 | +const data = makeData(50_000) |
| 59 | + |
| 60 | +@customElement('lit-table-example') |
| 61 | +class LitTableExample extends LitElement { |
| 62 | + private tableController = new TableController<Person>(this) |
| 63 | + |
| 64 | + protected render(): unknown { |
| 65 | + const table = this.tableController.table({ |
| 66 | + columns, |
| 67 | + data, |
| 68 | + getSortedRowModel: getSortedRowModel(), |
| 69 | + getCoreRowModel: getCoreRowModel(), |
| 70 | + }) |
| 71 | + const { rows } = table.getRowModel() |
| 72 | + return html` |
| 73 | + <div class="app"> |
| 74 | + (${data.length} rows) |
| 75 | + <div |
| 76 | + class="container" |
| 77 | + style="${styleMap({ |
| 78 | + overflow: 'auto', //our scrollable table container |
| 79 | + position: 'relative', //needed for sticky header |
| 80 | + height: '800px', //should be a fixed height |
| 81 | + })}" |
| 82 | + > |
| 83 | + <table style="display: grid"> |
| 84 | + <thead |
| 85 | + style="${styleMap({ |
| 86 | + display: 'grid', |
| 87 | + position: 'sticky', |
| 88 | + top: 0, |
| 89 | + zIndex: 1, |
| 90 | + })}" |
| 91 | + > |
| 92 | + ${repeat( |
| 93 | + table.getHeaderGroups(), |
| 94 | + headerGroup => headerGroup.id, |
| 95 | + headerGroup => html` |
| 96 | + <tr style="${styleMap({ display: 'flex', width: '100%' })}"> |
| 97 | + ${repeat( |
| 98 | + headerGroup.headers, |
| 99 | + header => header.id, |
| 100 | + header => html` |
| 101 | + <th |
| 102 | + style="${styleMap({ |
| 103 | + display: 'flex', |
| 104 | + width: `${header.getSize()}px`, |
| 105 | + })}" |
| 106 | + @click="${header.column.getToggleSortingHandler()}" |
| 107 | + > |
| 108 | + ${flexRender( |
| 109 | + header.column.columnDef.header, |
| 110 | + header.getContext() |
| 111 | + )} |
| 112 | + ${{ asc: ' 🔼', desc: ' 🔽' }[ |
| 113 | + header.column.getIsSorted() as string |
| 114 | + ] ?? null} |
| 115 | + </th> |
| 116 | + ` |
| 117 | + )} |
| 118 | + </tr> |
| 119 | + ` |
| 120 | + )} |
| 121 | + </thead> |
| 122 | + <tbody |
| 123 | + style=${styleMap({ |
| 124 | + display: 'grid', |
| 125 | + height: 500, |
| 126 | + // height: `${rowVirtualizer.getTotalSize()}px`, //tells scrollbar how big the table is |
| 127 | + position: 'relative', //needed for absolute positioning of rows |
| 128 | + })} |
| 129 | + > |
| 130 | + ${virtualize({ |
| 131 | + items: data, |
| 132 | + renderItem: (_, index) => { |
| 133 | + const row = rows[index] |
| 134 | + return html` |
| 135 | + <tr |
| 136 | + style=${styleMap({ |
| 137 | + display: 'flex', |
| 138 | + width: '100%', |
| 139 | + })} |
| 140 | + > |
| 141 | + ${repeat( |
| 142 | + row.getVisibleCells(), |
| 143 | + cell => cell.id, |
| 144 | + cell => html` |
| 145 | + <td |
| 146 | + style=${styleMap({ |
| 147 | + display: 'flex', |
| 148 | + width: `${cell.column.getSize()}px`, |
| 149 | + })} |
| 150 | + > |
| 151 | + ${flexRender( |
| 152 | + cell.column.columnDef.cell, |
| 153 | + cell.getContext() |
| 154 | + )} |
| 155 | + </td> |
| 156 | + ` |
| 157 | + )} |
| 158 | + </tr> |
| 159 | + ` |
| 160 | + }, |
| 161 | + })} |
| 162 | + </tbody> |
| 163 | + </table> |
| 164 | + </div> |
| 165 | + </div> |
| 166 | +
|
| 167 | + <style> |
| 168 | + html { |
| 169 | + font-family: sans-serif; |
| 170 | + font-size: 14px; |
| 171 | + } |
| 172 | +
|
| 173 | + table { |
| 174 | + border-collapse: collapse; |
| 175 | + border-spacing: 0; |
| 176 | + font-family: arial, sans-serif; |
| 177 | + table-layout: fixed; |
| 178 | + } |
| 179 | +
|
| 180 | + thead { |
| 181 | + background: lightgray; |
| 182 | + } |
| 183 | +
|
| 184 | + tr { |
| 185 | + border-bottom: 1px solid lightgray; |
| 186 | + } |
| 187 | +
|
| 188 | + th { |
| 189 | + border-bottom: 1px solid lightgray; |
| 190 | + border-right: 1px solid lightgray; |
| 191 | + padding: 2px 4px; |
| 192 | + text-align: left; |
| 193 | + } |
| 194 | +
|
| 195 | + td { |
| 196 | + padding: 6px; |
| 197 | + } |
| 198 | +
|
| 199 | + .container { |
| 200 | + border: 1px solid lightgray; |
| 201 | + margin: 1rem auto; |
| 202 | + } |
| 203 | +
|
| 204 | + .app { |
| 205 | + margin: 1rem auto; |
| 206 | + text-align: center; |
| 207 | + } |
| 208 | + </style> |
| 209 | + ` |
| 210 | + } |
| 211 | +} |
0 commit comments