|
| 1 | +import { customElement } from 'lit/decorators.js' |
| 2 | +import { |
| 3 | + html, |
| 4 | + LitElement |
| 5 | +} from 'lit' |
| 6 | +import { |
| 7 | + ColumnDef, |
| 8 | + flexRender, |
| 9 | + getCoreRowModel, |
| 10 | + getSortedRowModel, |
| 11 | + SortingFn, |
| 12 | + type SortingState, |
| 13 | + TableController, |
| 14 | +} from '@tanstack/lit-table' |
| 15 | +import { repeat } from 'lit/directives/repeat.js' |
| 16 | + |
| 17 | +import { makeData, Person } from './makeData.ts' |
| 18 | +import { state } from 'lit/decorators/state.js' |
| 19 | + |
| 20 | +const sortStatusFn: SortingFn<Person> = (rowA, rowB, _columnId) => { |
| 21 | + const statusA = rowA.original.status |
| 22 | + const statusB = rowB.original.status |
| 23 | + const statusOrder = ['single', 'complicated', 'relationship'] |
| 24 | + return statusOrder.indexOf(statusA) - statusOrder.indexOf(statusB) |
| 25 | +} |
| 26 | + |
| 27 | +const columns: ColumnDef<Person>[] = [ |
| 28 | + { |
| 29 | + accessorKey: 'firstName', |
| 30 | + cell: info => info.getValue(), |
| 31 | + //this column will sort in ascending order by default since it is a string column |
| 32 | + }, |
| 33 | + { |
| 34 | + accessorFn: row => row.lastName, |
| 35 | + id: 'lastName', |
| 36 | + cell: info => info.getValue(), |
| 37 | + header: () => html`<span>Last Name</span>`, |
| 38 | + sortUndefined: 'last', //force undefined values to the end |
| 39 | + sortDescFirst: false, //first sort order will be ascending (nullable values can mess up auto detection of sort order) |
| 40 | + }, |
| 41 | + { |
| 42 | + accessorKey: 'age', |
| 43 | + header: () => 'Age', |
| 44 | + //this column will sort in descending order by default since it is a number column |
| 45 | + }, |
| 46 | + { |
| 47 | + accessorKey: 'visits', |
| 48 | + header: () => html`<span>Visits</span>`, |
| 49 | + sortUndefined: 'last', //force undefined values to the end |
| 50 | + }, |
| 51 | + { |
| 52 | + accessorKey: 'status', |
| 53 | + header: 'Status', |
| 54 | + sortingFn: sortStatusFn, //use our custom sorting function for this enum column |
| 55 | + }, |
| 56 | + { |
| 57 | + accessorKey: 'progress', |
| 58 | + header: 'Profile Progress', |
| 59 | + // enableSorting: false, //disable sorting for this column |
| 60 | + }, |
| 61 | + { |
| 62 | + accessorKey: 'rank', |
| 63 | + header: 'Rank', |
| 64 | + invertSorting: true, //invert the sorting order (golf score-like where smaller is better) |
| 65 | + }, |
| 66 | + { |
| 67 | + accessorKey: 'createdAt', |
| 68 | + header: 'Created At', |
| 69 | + // sortingFn: 'datetime' //make sure table knows this is a datetime column (usually can detect if no null values) |
| 70 | + }, |
| 71 | +] |
| 72 | + |
| 73 | +const data: Person[] = makeData(100_000) |
| 74 | + |
| 75 | +@customElement('lit-table-example') |
| 76 | +class LitTableExample extends LitElement { |
| 77 | + @state() |
| 78 | + private _sorting: SortingState = [] |
| 79 | + |
| 80 | + private tableController = new TableController<Person>(this) |
| 81 | + |
| 82 | + protected render(): unknown { |
| 83 | + const table = this.tableController.getTable({ |
| 84 | + columns, |
| 85 | + data, |
| 86 | + state: { |
| 87 | + sorting: this._sorting, |
| 88 | + }, |
| 89 | + onSortingChange: updaterOrValue => { |
| 90 | + if (typeof updaterOrValue === 'function') { |
| 91 | + this._sorting = updaterOrValue(this._sorting) |
| 92 | + } else { |
| 93 | + this._sorting = updaterOrValue |
| 94 | + } |
| 95 | + }, |
| 96 | + getSortedRowModel: getSortedRowModel(), |
| 97 | + getCoreRowModel: getCoreRowModel(), |
| 98 | + }) |
| 99 | + |
| 100 | + return html` |
| 101 | + <table> |
| 102 | + <thead> |
| 103 | + ${repeat( |
| 104 | + table.getHeaderGroups(), |
| 105 | + headerGroup => headerGroup.id, |
| 106 | + headerGroup => html` |
| 107 | + <tr> |
| 108 | + ${repeat( |
| 109 | + headerGroup.headers, |
| 110 | + header => header.id, |
| 111 | + header => html` |
| 112 | + <th colspan="${header.colSpan}"> |
| 113 | + ${header.isPlaceholder |
| 114 | + ? null |
| 115 | + : html`<div |
| 116 | + title=${header.column.getCanSort() |
| 117 | + ? header.column.getNextSortingOrder() === 'asc' |
| 118 | + ? 'Sort ascending' |
| 119 | + : header.column.getNextSortingOrder() === 'desc' |
| 120 | + ? 'Sort descending' |
| 121 | + : 'Clear sort' |
| 122 | + : undefined} |
| 123 | + @click="${header.column.getToggleSortingHandler()}" |
| 124 | + style="cursor: ${header.column.getCanSort() |
| 125 | + ? 'pointer' |
| 126 | + : 'not-allowed'}" |
| 127 | + > |
| 128 | + ${flexRender( |
| 129 | + header.column.columnDef.header, |
| 130 | + header.getContext() |
| 131 | + )} |
| 132 | + ${{ asc: ' 🔼', desc: ' 🔽' }[ |
| 133 | + header.column.getIsSorted() as string |
| 134 | + ] ?? null} |
| 135 | + </div>`} |
| 136 | + </th> |
| 137 | + ` |
| 138 | + )} |
| 139 | + </tr> |
| 140 | + ` |
| 141 | + )} |
| 142 | + </thead> |
| 143 | + <tbody> |
| 144 | + ${repeat( |
| 145 | + table.getRowModel().rows.slice(0, 10), |
| 146 | + row => row.id, |
| 147 | + row => html` |
| 148 | + <tr> |
| 149 | + ${repeat( |
| 150 | + row.getVisibleCells(), |
| 151 | + cell => cell.id, |
| 152 | + cell => html` |
| 153 | + <td> |
| 154 | + ${flexRender( |
| 155 | + cell.column.columnDef.cell, |
| 156 | + cell.getContext() |
| 157 | + )} |
| 158 | + </td> |
| 159 | + ` |
| 160 | + )} |
| 161 | + </tr> |
| 162 | + ` |
| 163 | + )} |
| 164 | + </tbody> |
| 165 | + </table> |
| 166 | + <pre>${JSON.stringify(this._sorting, null, 2)}</pre> |
| 167 | + <style> |
| 168 | + * { |
| 169 | + font-family: sans-serif; |
| 170 | + font-size: 14px; |
| 171 | + box-sizing: border-box; |
| 172 | + } |
| 173 | +
|
| 174 | + table { |
| 175 | + border: 1px solid lightgray; |
| 176 | + border-collapse: collapse; |
| 177 | + } |
| 178 | +
|
| 179 | + tbody { |
| 180 | + border-bottom: 1px solid lightgray; |
| 181 | + } |
| 182 | +
|
| 183 | + th { |
| 184 | + border-bottom: 1px solid lightgray; |
| 185 | + border-right: 1px solid lightgray; |
| 186 | + padding: 2px 4px; |
| 187 | + } |
| 188 | +
|
| 189 | + tfoot { |
| 190 | + color: gray; |
| 191 | + } |
| 192 | +
|
| 193 | + tfoot th { |
| 194 | + font-weight: normal; |
| 195 | + } |
| 196 | + </style> |
| 197 | + ` |
| 198 | + } |
| 199 | +} |
0 commit comments