|
| 1 | +<script lang="ts"> |
| 2 | + import { |
| 3 | + createTableHelper, |
| 4 | + FlexRender, |
| 5 | + } from '@tanstack/svelte-table' |
| 6 | + import './index.css' |
| 7 | +
|
| 8 | + // This example uses the new `createTableHelper` method to create a re-usable table helper object instead of independently using the standalone `createTable` hook and `createColumnHelper` method. You can choose to use either way. |
| 9 | +
|
| 10 | + // 1. Define what the shape of your data will be for each row |
| 11 | + type Person = { |
| 12 | + firstName: string |
| 13 | + lastName: string |
| 14 | + age: number |
| 15 | + visits: number |
| 16 | + status: string |
| 17 | + progress: number |
| 18 | + } |
| 19 | +
|
| 20 | + // 2. Create some dummy data with a stable reference (this could be an API response stored in useState or similar) |
| 21 | + const data: Person[] = [ |
| 22 | + { |
| 23 | + firstName: 'tanner', |
| 24 | + lastName: 'linsley', |
| 25 | + age: 24, |
| 26 | + visits: 100, |
| 27 | + status: 'In Relationship', |
| 28 | + progress: 50, |
| 29 | + }, |
| 30 | + { |
| 31 | + firstName: 'tandy', |
| 32 | + lastName: 'miller', |
| 33 | + age: 40, |
| 34 | + visits: 40, |
| 35 | + status: 'Single', |
| 36 | + progress: 80, |
| 37 | + }, |
| 38 | + { |
| 39 | + firstName: 'joe', |
| 40 | + lastName: 'dirte', |
| 41 | + age: 45, |
| 42 | + visits: 20, |
| 43 | + status: 'Complicated', |
| 44 | + progress: 10, |
| 45 | + }, |
| 46 | + ] |
| 47 | +
|
| 48 | + // 3. New in V9! Tell the table which features and row models we want to use. In this case, this will be a basic table with no additional features |
| 49 | + const tableHelper = createTableHelper({ |
| 50 | + _features: {}, |
| 51 | + _rowModels: {}, // client-side row models. `Core` row model is now included by default, but you can still override it here |
| 52 | + _processingFns: {}, // client-side processing functions used by the row models (sorting, filtering, etc.). Not needed in this basic example |
| 53 | + TData: {} as Person, |
| 54 | + debugTable: true, |
| 55 | + }) |
| 56 | +
|
| 57 | + // 4. Create a helper object to help define our columns |
| 58 | + const { columnHelper } = tableHelper |
| 59 | +
|
| 60 | + // 5. Define the columns for your table with a stable reference (in this case, defined statically outside of a react component) |
| 61 | + const columns = columnHelper.columns([ |
| 62 | + // accessorKey method (most common for simple use-cases) |
| 63 | + columnHelper.accessor('firstName', { |
| 64 | + cell: (info) => info.getValue(), |
| 65 | + footer: (info) => info.column.id, |
| 66 | + }), |
| 67 | + // accessorFn used (alternative) along with a custom id |
| 68 | + columnHelper.accessor((row) => row.lastName, { |
| 69 | + id: 'lastName', |
| 70 | + cell: (info) => info.getValue(), |
| 71 | + header: () => 'Last Name', |
| 72 | + footer: (info) => info.column.id, |
| 73 | + }), |
| 74 | + // accessorFn used to transform the data |
| 75 | + columnHelper.accessor((row) => Number(row.age), { |
| 76 | + id: 'age', |
| 77 | + header: () => 'Age', |
| 78 | + cell: (info) => info.renderValue(), |
| 79 | + footer: (info) => info.column.id, |
| 80 | + }), |
| 81 | + columnHelper.accessor('visits', { |
| 82 | + header: () => 'Visits', |
| 83 | + footer: (info) => info.column.id, |
| 84 | + }), |
| 85 | + columnHelper.accessor('status', { |
| 86 | + header: 'Status', |
| 87 | + footer: (info) => info.column.id, |
| 88 | + }), |
| 89 | + columnHelper.accessor('progress', { |
| 90 | + header: 'Profile Progress', |
| 91 | + footer: (info) => info.column.id, |
| 92 | + }), |
| 93 | + ]) |
| 94 | +
|
| 95 | + // 7. Create the table instance with the required columns and data. |
| 96 | + // Features and row models are already defined in the table helper object above |
| 97 | + const table = tableHelper.createTable({ |
| 98 | + columns, |
| 99 | + data, |
| 100 | + // add additional table options here or in the table helper above |
| 101 | + }) |
| 102 | +</script> |
| 103 | + |
| 104 | +<div class="p-2"> |
| 105 | + <table> |
| 106 | + <thead> |
| 107 | + {#each table.getHeaderGroups() as headerGroup} |
| 108 | + <tr> |
| 109 | + {#each headerGroup.headers as header} |
| 110 | + <th> |
| 111 | + {#if !header.isPlaceholder} |
| 112 | + <FlexRender |
| 113 | + content={header.column.columnDef.header} |
| 114 | + context={header.getContext()} |
| 115 | + /> |
| 116 | + {/if} |
| 117 | + </th> |
| 118 | + {/each} |
| 119 | + </tr> |
| 120 | + {/each} |
| 121 | + </thead> |
| 122 | + <tbody> |
| 123 | + {#each table.getRowModel().rows as row} |
| 124 | + <tr> |
| 125 | + {#each row.getAllCells() as cell} |
| 126 | + <td> |
| 127 | + <FlexRender |
| 128 | + content={cell.column.columnDef.cell} |
| 129 | + context={cell.getContext()} |
| 130 | + /> |
| 131 | + </td> |
| 132 | + {/each} |
| 133 | + </tr> |
| 134 | + {/each} |
| 135 | + </tbody> |
| 136 | + <tfoot> |
| 137 | + {#each table.getFooterGroups() as footerGroup} |
| 138 | + <tr> |
| 139 | + {#each footerGroup.headers as header} |
| 140 | + <th> |
| 141 | + {#if !header.isPlaceholder} |
| 142 | + <FlexRender |
| 143 | + content={header.column.columnDef.footer} |
| 144 | + context={header.getContext()} |
| 145 | + /> |
| 146 | + {/if} |
| 147 | + </th> |
| 148 | + {/each} |
| 149 | + </tr> |
| 150 | + {/each} |
| 151 | + </tfoot> |
| 152 | + </table> |
| 153 | + <div class="h-4"></div> |
| 154 | +</div> |
0 commit comments