|
| 1 | +import './index.css' |
| 2 | + |
| 3 | +import { |
| 4 | + type SortingFn, |
| 5 | + createColumnHelper, |
| 6 | + getCoreRowModel, |
| 7 | + getSortedRowModel, |
| 8 | +} from '@tanstack/table-core' |
| 9 | + |
| 10 | +import { makeData, Person } from './makeData' |
| 11 | +import { flexRender, useTable } from './useTable' |
| 12 | + |
| 13 | +const data = makeData(1000) |
| 14 | + |
| 15 | +// Custom sorting logic for one of our enum columns |
| 16 | +const sortStatusFn: SortingFn<Person> = (rowA, rowB, _columnId) => { |
| 17 | + const statusA = rowA.original.status |
| 18 | + const statusB = rowB.original.status |
| 19 | + const statusOrder = ['single', 'complicated', 'relationship'] |
| 20 | + return statusOrder.indexOf(statusA) - statusOrder.indexOf(statusB) |
| 21 | +} |
| 22 | + |
| 23 | +const columnHelper = createColumnHelper<Person>() |
| 24 | + |
| 25 | +const columns = [ |
| 26 | + columnHelper.accessor('firstName', { |
| 27 | + cell: info => info.getValue(), |
| 28 | + // This column will sort in ascending order by default since it is a string column |
| 29 | + }), |
| 30 | + columnHelper.accessor(row => row.lastName, { |
| 31 | + id: 'lastName', |
| 32 | + cell: info => `<i>${info.getValue()}</i>`, |
| 33 | + header: () => '<span>Last Name</span>', |
| 34 | + sortUndefined: 'last', // Force undefined values to the end |
| 35 | + sortDescFirst: false, // First sort order will be ascending (nullable values can mess up auto detection of sort order) |
| 36 | + }), |
| 37 | + columnHelper.accessor('age', { |
| 38 | + header: () => 'Age', |
| 39 | + cell: info => info.renderValue(), |
| 40 | + // This column will sort in descending order by default since it is a number column |
| 41 | + }), |
| 42 | + columnHelper.accessor('visits', { |
| 43 | + header: () => '<span>Visits</span>', |
| 44 | + sortUndefined: 'last', // Force undefined values to the end |
| 45 | + }), |
| 46 | + columnHelper.accessor('status', { |
| 47 | + header: 'Status', |
| 48 | + sortingFn: sortStatusFn, // Use our custom sorting function for this enum column |
| 49 | + }), |
| 50 | + columnHelper.accessor('progress', { |
| 51 | + header: 'Profile Progress', |
| 52 | + enableSorting: false, // Disable sorting for this column |
| 53 | + }), |
| 54 | + columnHelper.accessor('rank', { |
| 55 | + header: 'Rank', |
| 56 | + invertSorting: true, // Invert the sorting order (golf score-like where smaller is better) |
| 57 | + }), |
| 58 | + columnHelper.accessor('createdAt', { |
| 59 | + header: 'Created At', |
| 60 | + }), |
| 61 | +] |
| 62 | + |
| 63 | +const renderTable = () => { |
| 64 | + // Create table elements |
| 65 | + const tableElement = document.createElement('table') |
| 66 | + const theadElement = document.createElement('thead') |
| 67 | + const tbodyElement = document.createElement('tbody') |
| 68 | + |
| 69 | + tableElement.classList.add('mb-2') |
| 70 | + |
| 71 | + tableElement.appendChild(theadElement) |
| 72 | + tableElement.appendChild(tbodyElement) |
| 73 | + |
| 74 | + // Render table headers |
| 75 | + table.getHeaderGroups().forEach(headerGroup => { |
| 76 | + const trElement = document.createElement('tr') |
| 77 | + headerGroup.headers.forEach(header => { |
| 78 | + const thElement = document.createElement('th') |
| 79 | + thElement.colSpan = header.colSpan |
| 80 | + const divElement = document.createElement('div') |
| 81 | + divElement.classList.add( |
| 82 | + 'w-36', |
| 83 | + ...(header.column.getCanSort() ? ['cursor-pointer', 'select-none'] : []) |
| 84 | + ) |
| 85 | + ;(divElement.onclick = e => header.column.getToggleSortingHandler()?.(e)), |
| 86 | + (divElement.innerHTML = header.isPlaceholder |
| 87 | + ? '' |
| 88 | + : flexRender(header.column.columnDef.header, header.getContext())) |
| 89 | + divElement.innerHTML += |
| 90 | + { |
| 91 | + asc: ' 🔼', |
| 92 | + desc: ' 🔽', |
| 93 | + }[header.column.getIsSorted() as string] ?? '' |
| 94 | + thElement.appendChild(divElement) |
| 95 | + trElement.appendChild(thElement) |
| 96 | + }) |
| 97 | + theadElement.appendChild(trElement) |
| 98 | + }) |
| 99 | + |
| 100 | + // Render table rows |
| 101 | + table |
| 102 | + .getRowModel() |
| 103 | + .rows.slice(0, 10) |
| 104 | + .forEach(row => { |
| 105 | + const trElement = document.createElement('tr') |
| 106 | + row.getVisibleCells().forEach(cell => { |
| 107 | + const tdElement = document.createElement('td') |
| 108 | + tdElement.innerHTML = flexRender( |
| 109 | + cell.column.columnDef.cell, |
| 110 | + cell.getContext() |
| 111 | + ) |
| 112 | + trElement.appendChild(tdElement) |
| 113 | + }) |
| 114 | + tbodyElement.appendChild(trElement) |
| 115 | + }) |
| 116 | + |
| 117 | + // Render table state info |
| 118 | + const stateInfoElement = document.createElement('pre') |
| 119 | + stateInfoElement.textContent = JSON.stringify( |
| 120 | + { |
| 121 | + sorting: table.getState().sorting, |
| 122 | + }, |
| 123 | + null, |
| 124 | + 2 |
| 125 | + ) |
| 126 | + |
| 127 | + // Clear previous content and append new content |
| 128 | + const wrapperElement = document.getElementById('wrapper') as HTMLDivElement |
| 129 | + wrapperElement.innerHTML = '' |
| 130 | + wrapperElement.appendChild(tableElement) |
| 131 | + wrapperElement.appendChild(stateInfoElement) |
| 132 | +} |
| 133 | + |
| 134 | +const table = useTable<Person>({ |
| 135 | + data, |
| 136 | + columns, |
| 137 | + getCoreRowModel: getCoreRowModel(), |
| 138 | + getSortedRowModel: getSortedRowModel(), |
| 139 | + onStateChange: () => renderTable(), |
| 140 | +}) |
| 141 | + |
| 142 | +renderTable() |
0 commit comments