|
| 1 | +import { ChangeDetectionStrategy, Component, signal } from '@angular/core' |
| 2 | +import { FlexRender, createTableHook } from '@tanstack/angular-table' |
| 3 | + |
| 4 | +// This example uses the new `createTableHook` method to create a re-usable table hook factory instead of independently |
| 5 | +// using the standalone `useTable` hook and `createColumnHelper` method. You can choose to use either way. |
| 6 | + |
| 7 | +// 1. Define what the shape of your data will be for each row |
| 8 | +type Person = { |
| 9 | + firstName: string |
| 10 | + lastName: string |
| 11 | + age: number |
| 12 | + visits: number |
| 13 | + status: string |
| 14 | + progress: number |
| 15 | +} |
| 16 | + |
| 17 | +// 2. Create some dummy data with a stable reference (this could be an API response stored in useState or similar) |
| 18 | +const defaultData: Array<Person> = [ |
| 19 | + { |
| 20 | + firstName: 'tanner', |
| 21 | + lastName: 'linsley', |
| 22 | + age: 24, |
| 23 | + visits: 100, |
| 24 | + status: 'In Relationship', |
| 25 | + progress: 50, |
| 26 | + }, |
| 27 | + { |
| 28 | + firstName: 'tandy', |
| 29 | + lastName: 'miller', |
| 30 | + age: 40, |
| 31 | + visits: 40, |
| 32 | + status: 'Single', |
| 33 | + progress: 80, |
| 34 | + }, |
| 35 | + { |
| 36 | + firstName: 'joe', |
| 37 | + lastName: 'dirte', |
| 38 | + age: 45, |
| 39 | + visits: 20, |
| 40 | + status: 'Complicated', |
| 41 | + progress: 10, |
| 42 | + }, |
| 43 | + { |
| 44 | + firstName: 'kevin', |
| 45 | + lastName: 'vandy', |
| 46 | + age: 28, |
| 47 | + visits: 100, |
| 48 | + status: 'Single', |
| 49 | + progress: 70, |
| 50 | + }, |
| 51 | +] |
| 52 | + |
| 53 | +// 3. New in V9! Tell the table which features and row models we want to use. |
| 54 | +// In this case, this will be a basic table with no additional features |
| 55 | +const { injectAppTable, createAppColumnHelper } = createTableHook({ |
| 56 | + _features: {}, |
| 57 | + _rowModels: {}, // client-side row models. `Core` row model is now included by default, but you can still override it here |
| 58 | + debugTable: true, |
| 59 | +}) |
| 60 | + |
| 61 | +// 4. Create a helper object to help define our columns |
| 62 | +const columnHelper = createAppColumnHelper<Person>() |
| 63 | + |
| 64 | +// 5. Define the columns for your table with a stable reference (in this case, defined statically outside of a react component) |
| 65 | +const columns = columnHelper.columns([ |
| 66 | + // accessorKey method (most common for simple use-cases) |
| 67 | + columnHelper.accessor('firstName', { |
| 68 | + cell: (info) => info.getValue(), |
| 69 | + footer: (info) => info.column.id, |
| 70 | + }), |
| 71 | + // accessorFn used (alternative) along with a custom id |
| 72 | + columnHelper.accessor((row) => row.lastName, { |
| 73 | + id: 'lastName', |
| 74 | + cell: (info) => `<i>${info.getValue()}</i>`, |
| 75 | + header: () => `<span>Last Name</span>`, |
| 76 | + footer: (info) => info.column.id, |
| 77 | + }), |
| 78 | + // accessorFn used to transform the data |
| 79 | + columnHelper.accessor((row) => Number(row.age), { |
| 80 | + id: 'age', |
| 81 | + header: () => 'Age', |
| 82 | + cell: (info) => info.renderValue(), |
| 83 | + footer: (info) => info.column.id, |
| 84 | + }), |
| 85 | + columnHelper.accessor('visits', { |
| 86 | + header: () => `<span>Visits</span>`, |
| 87 | + footer: (info) => info.column.id, |
| 88 | + }), |
| 89 | + columnHelper.accessor('status', { |
| 90 | + header: 'Status', |
| 91 | + footer: (info) => info.column.id, |
| 92 | + }), |
| 93 | + columnHelper.accessor('progress', { |
| 94 | + header: 'Profile Progress', |
| 95 | + footer: (info) => info.column.id, |
| 96 | + }), |
| 97 | +]) |
| 98 | + |
| 99 | +@Component({ |
| 100 | + selector: 'app-root', |
| 101 | + imports: [FlexRender], |
| 102 | + templateUrl: './app.component.html', |
| 103 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 104 | +}) |
| 105 | +export class AppComponent { |
| 106 | + readonly data = signal<Array<Person>>(defaultData) |
| 107 | + |
| 108 | + // 6. Create the table instance with the required columns and data. |
| 109 | + // Features and row models are already defined in the createTableHook call above |
| 110 | + readonly table = injectAppTable(() => ({ |
| 111 | + columns, |
| 112 | + data: this.data(), |
| 113 | + // add additional table options here or in the createTableHook call above |
| 114 | + })) |
| 115 | + |
| 116 | + rerender() { |
| 117 | + this.data.set([...defaultData.sort(() => -1)]) |
| 118 | + } |
| 119 | +} |
0 commit comments