|
| 1 | +<script lang="ts"> |
| 2 | + import { |
| 3 | + createTableHelper, |
| 4 | + FlexRender, |
| 5 | + renderSnippet, |
| 6 | + } from '@tanstack/svelte-table' |
| 7 | + import { capitalized, countup, spectrum } from './snippets.svelte' |
| 8 | + import './index.css' |
| 9 | +
|
| 10 | + /** |
| 11 | + * This `svelte-table` example demonstrates the following: |
| 12 | + * - Creating a basic table with no additional features (sorting, filtering, |
| 13 | + * grouping, etc), |
| 14 | + * - Creating and using a `table helper`, |
| 15 | + * - Defining columns with custom headers, cells, and footers using the table |
| 16 | + * helper, and |
| 17 | + * - Rendering a table with the instance APIs. |
| 18 | + */ |
| 19 | +
|
| 20 | + // 1. Define what the shape of your data will be for each row |
| 21 | + type Person = { |
| 22 | + firstName: string |
| 23 | + lastName: string |
| 24 | + age: number |
| 25 | + visits: number |
| 26 | + status: string |
| 27 | + progress: number |
| 28 | + } |
| 29 | +
|
| 30 | + // 2. Create some dummy data with a stable reference (this could be an API |
| 31 | + // response stored in a rune). |
| 32 | + const data: Person[] = [ |
| 33 | + { |
| 34 | + firstName: 'tanner', |
| 35 | + lastName: 'linsley', |
| 36 | + age: 24, |
| 37 | + visits: 100, |
| 38 | + status: 'In Relationship', |
| 39 | + progress: 50, |
| 40 | + }, |
| 41 | + { |
| 42 | + firstName: 'tandy', |
| 43 | + lastName: 'miller', |
| 44 | + age: 40, |
| 45 | + visits: 40, |
| 46 | + status: 'Single', |
| 47 | + progress: 80, |
| 48 | + }, |
| 49 | + { |
| 50 | + firstName: 'joe', |
| 51 | + lastName: 'dirte', |
| 52 | + age: 45, |
| 53 | + visits: 20, |
| 54 | + status: 'Complicated', |
| 55 | + progress: 10, |
| 56 | + }, |
| 57 | + ] |
| 58 | +
|
| 59 | + // 3. New in V9! Tell the table which features and row models we want to use. |
| 60 | + // In this case, this will be a basic table with no additional features |
| 61 | + const tableHelper = createTableHelper({ |
| 62 | + _features: {}, |
| 63 | + // 3a. `_rowModels` defines client-side row models. `Core` row model is now |
| 64 | + // included by default, but you can still override it here. |
| 65 | + _rowModels: {}, |
| 66 | + // 3b. Optionally, set the `TData` type. Omit TData is this table helper |
| 67 | + // will be used to create multiple tables with different data types. |
| 68 | + TData: {} as Person, |
| 69 | + }) |
| 70 | +
|
| 71 | + // 4. For convenience, destructure the desired utilities from `tableHelper` |
| 72 | + const { columnHelper, createTable } = tableHelper |
| 73 | +
|
| 74 | + // 5. Define the columns for your table with a stable reference (in this case, |
| 75 | + // defined statically). |
| 76 | + const columns = columnHelper.columns([ |
| 77 | + columnHelper.accessor('firstName', { |
| 78 | + header: 'First Name', |
| 79 | + // 5a. Use the `renderSnippet` utility to render the snippet in the cell. |
| 80 | + cell: (info) => renderSnippet(capitalized, info.getValue()), |
| 81 | + }), |
| 82 | + columnHelper.accessor('lastName', { |
| 83 | + header: 'Last Name', |
| 84 | + cell: (info) => renderSnippet(capitalized, info.getValue()), |
| 85 | + }), |
| 86 | + columnHelper.accessor('age', { |
| 87 | + header: 'Age', |
| 88 | + cell: (info) => renderSnippet(countup, info.getValue()), |
| 89 | + }), |
| 90 | + columnHelper.accessor('visits', { |
| 91 | + header: 'Visits', |
| 92 | + cell: (info) => renderSnippet(countup, info.getValue()), |
| 93 | + }), |
| 94 | + columnHelper.accessor('status', { |
| 95 | + header: 'Status', |
| 96 | + }), |
| 97 | + columnHelper.accessor('progress', { |
| 98 | + header: 'Profile Progress', |
| 99 | + cell(info) { |
| 100 | + return renderSnippet(spectrum, { |
| 101 | + value: info.getValue(), |
| 102 | + min: 0, |
| 103 | + max: 100, |
| 104 | + }) |
| 105 | + }, |
| 106 | + }), |
| 107 | + ]) |
| 108 | +
|
| 109 | + // 6. Create the table instance with columns and data. Features and row |
| 110 | + // models are already defined in the `tableHelper` object that |
| 111 | + // `createTable` was destructured from. |
| 112 | + const table = createTable({ |
| 113 | + columns, |
| 114 | + data, |
| 115 | + }) |
| 116 | +</script> |
| 117 | + |
| 118 | +<!-- 7. Render the table in markup using the Instance APIs. --> |
| 119 | +<div class="p-2"> |
| 120 | + <table> |
| 121 | + <thead> |
| 122 | + {#each table.getHeaderGroups() as headerGroup} |
| 123 | + <tr> |
| 124 | + {#each headerGroup.headers as header} |
| 125 | + <th> |
| 126 | + {#if !header.isPlaceholder} |
| 127 | + <FlexRender |
| 128 | + content={header.column.columnDef.header} |
| 129 | + context={header.getContext()} |
| 130 | + /> |
| 131 | + {/if} |
| 132 | + </th> |
| 133 | + {/each} |
| 134 | + </tr> |
| 135 | + {/each} |
| 136 | + </thead> |
| 137 | + <tbody> |
| 138 | + {#each table.getRowModel().rows as row} |
| 139 | + <tr> |
| 140 | + {#each row.getAllCells() as cell} |
| 141 | + <td> |
| 142 | + <FlexRender |
| 143 | + content={cell.column.columnDef.cell} |
| 144 | + context={cell.getContext()} |
| 145 | + /> |
| 146 | + </td> |
| 147 | + {/each} |
| 148 | + </tr> |
| 149 | + {/each} |
| 150 | + </tbody> |
| 151 | + </table> |
| 152 | +</div> |
0 commit comments