You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -10,15 +10,15 @@ TanStack Table has a simple underlying internal state management system to store
10
10
11
11
You do not need to set up anything special in order for the table state to work. If you pass nothing into either `state`, `initialState`, or any of the `on[State]Change` table options, the table will manage its own state internally. You can access any part of this internal state by using the `table.getState()` table instance API.
12
12
13
-
```jsx
14
-
constoptions=writable({
13
+
```ts
14
+
consttable=createTable({
15
15
columns,
16
-
data,
16
+
get data() {
17
+
returndata
18
+
},
17
19
//...
18
20
})
19
21
20
-
consttable=createTable(options)
21
-
22
22
console.log(table.getState()) //access the entire internal state
23
23
console.log(table.getState().rowSelection) //access just the row selection state
24
24
```
@@ -27,8 +27,8 @@ console.log(table.getState().rowSelection) //access just the row selection state
27
27
28
28
If all you need to do for certain states is customize their initial default values, you still do not need to manage any of the state yourself. You can simply set values in the `initialState` option of the table instance.
29
29
30
-
```jsx
31
-
constoptions=writable({
30
+
```ts
31
+
consttable=createTable({
32
32
columns,
33
33
data,
34
34
initialState: {
@@ -46,11 +46,9 @@ const options = writable({
46
46
},
47
47
//...
48
48
})
49
-
50
-
consttable=createTable(options)
51
49
```
52
50
53
-
> **Note**: Only specify each particular state in either `initialState` or `state`, but not both. If you pass in a particular state value to both `initialState` and `state`, the initialized state in `state` will take overwrite any corresponding value in `initialState`.
51
+
> **Note**: Only specify each particular state in either `initialState` or `state`, but not both. If you pass in a particular state value to both `initialState` and `state`, the initialized state in `state` will overwrite any corresponding value in `initialState`.
54
52
55
53
### Controlled State
56
54
@@ -65,161 +63,129 @@ In order to control a particular state, you need to both pass in the correspondi
65
63
Let's take filtering, sorting, and pagination as an example in a "manual" server-side data fetching scenario. You can store the filtering, sorting, and pagination state in your own state management, but leave out any other state like column order, column visibility, etc. if your API does not care about those values.
66
64
67
65
```ts
68
-
let sorting=[
66
+
let sorting:SortingState=$state([
69
67
{
70
68
id: 'age',
71
69
desc: true, //sort by age in descending order by default
72
70
},
73
-
]
74
-
const setSorting=updater=> {
71
+
])
72
+
function setSorting(updater:Updater<SortingState>) {
75
73
if (updaterinstanceofFunction) {
76
74
sorting=updater(sorting)
77
-
} else {
78
-
sorting=updater
79
-
}
80
-
options.update(old=> ({
81
-
...old,
82
-
state: {
83
-
...old.state,
84
-
sorting,
85
-
},
86
-
}))
75
+
} elsesorting=updater
87
76
}
88
77
89
-
let columnFilters= []//no default filters
90
-
const setColumnFilters=updater=> {
78
+
let columnFilters:ColumnFiltersState=$state([])//no default filters
79
+
function setColumnFilters(updater:Updater<ColumnFiltersState>) {
columnFilters, //pass controlled state back to the table (overrides internal state)
134
-
sorting,
135
-
pagination
103
+
get sorting() {
104
+
returnsorting
105
+
},
106
+
get columnFilters() {
107
+
returncolumnFilters
108
+
},
109
+
get pagination() {
110
+
returnpagination
111
+
}
136
112
},
137
113
onColumnFiltersChange: setColumnFilters, //hoist columnFilters state into our own state management
138
114
onSortingChange: setSorting,
139
115
onPaginationChange: setPagination,
140
-
})
116
+
}
141
117
142
118
const table =createTable(options)
143
119
//...
144
120
```
145
121
146
122
#### Fully Controlled State
147
123
148
-
Alternatively, you can control the entire table state with the `onStateChange` table option. It will hoist out the entire table state into your own state management system. Be careful with this approach, as you might find that raising some frequently changing state values up a svelte tree, like `columnSizingInfo` state`, might cause bad performance issues.
124
+
Alternatively, you can control the entire table state with the `onStateChange` table option. It will hoist out the entire table state into your own state management system. Be careful with this approach, as you might find that raising some frequently changing state values up a Svelte tree, like `columnSizingInfo` state, might cause bad performance issues.
149
125
150
126
A couple of more tricks may be needed to make this work. If you use the `onStateChange` table option, the initial values of the `state` must be populated with all of the relevant state values for all of the features that you want to use. You can either manually type out all of the initial state values, or use the `table.setOptions` API in a special way as shown below.
151
127
152
-
```jsx
128
+
```ts
153
129
//create a table instance with default state values
154
-
constoptions=writable({
130
+
consttable=createTable({
155
131
columns,
156
-
data,
132
+
get data() {
133
+
returndata
134
+
},
157
135
//... Note: `state` values are NOT passed in yet
158
136
})
159
-
consttable=createTable(options)
160
137
161
-
let state = {
138
+
const state =$state({
162
139
...table.initialState, //populate the initial state with all of the default state values from the table instance
163
140
pagination: {
164
141
pageIndex: 0,
165
142
pageSize: 15//optionally customize the initial pagination state.
166
143
}
167
-
}
144
+
})
145
+
168
146
const setState =updater=> {
169
147
if (updaterinstanceofFunction) {
170
148
state=updater(state)
171
-
} else {
172
-
state = updater
173
-
}
174
-
options.update(old=> ({
175
-
...old,
176
-
state,
177
-
}))
149
+
} state=updater
178
150
}
179
151
180
152
//Use the table.setOptions API to merge our fully controlled state onto the table instance
181
153
table.setOptions(prev=> ({
182
154
...prev, //preserve any other options that we have set up above
183
-
state, //our fully controlled state overrides the internal state
155
+
get state() {
156
+
returnstate//our fully controlled state overrides the internal state
157
+
},
184
158
onStateChange: setState//any state changes will be pushed up to our own state management
185
159
}))
186
160
```
187
161
188
162
### On State Change Callbacks
189
163
190
-
So far, we have seen the `on[State]Change` and `onStateChange` table options work to "hoist" the table state changes into our own state management. However, there are a few things about these using these options that you should be aware of.
164
+
So far, we have seen the `on[State]Change` and `onStateChange` table options work to "hoist" the table state changes into our own state management. However, there are a few things about using these options that you should be aware of.
191
165
192
166
#### 1. **State Change Callbacks MUST have their corresponding state value in the `state` option**.
193
167
194
168
Specifying an `on[State]Change` callback tells the table instance that this will be a controlled state. If you do not specify the corresponding `state` value, that state will be "frozen" with its initial value.
195
169
196
170
```ts
197
-
let sorting =[]
171
+
let sorting =$state([])
198
172
const setSorting =updater=> {
199
173
if (updaterinstanceofFunction) {
200
174
sorting=updater(sorting)
201
-
} else {
202
-
sorting=updater
203
-
}
204
-
options.update(old=> ({
205
-
...old,
206
-
state: {
207
-
...old.state,
208
-
sorting,
209
-
},
210
-
}))
175
+
} elsesorting=updater
211
176
}
212
-
//...
213
-
constoptions=writable({
177
+
//...
178
+
consttable=createTable({
214
179
columns,
215
180
data,
216
181
//...
217
182
state: {
218
-
sorting, //required because we are using `onSortingChange`
183
+
get sorting() {
184
+
returnsorting//required because we are using `onSortingChange`
185
+
},
219
186
},
220
187
onSortingChange: setSorting, //makes the `state.sorting` controlled
221
188
})
222
-
const table =createTable(options)
223
189
```
224
190
225
191
#### 2. **Updaters can either be raw values or callback functions**.
@@ -246,15 +212,6 @@ let sorting: SortingState[] = [
Copy file name to clipboardExpand all lines: docs/framework/svelte/svelte-table.md
+74-4Lines changed: 74 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,18 +2,88 @@
2
2
title: Svelte Table
3
3
---
4
4
5
-
The `@tanstack/svelte-table` adapter is a wrapper around the core table logic. Most of it's job is related to managing state the "svelte" way, providing types and the rendering implementation of cell/header/footer templates.
5
+
> **IMPORTANT:** This version of `@tanstack/svelte-table` only supports **Svelte 5 or
6
+
> newer**. For Svelte 3/4 support, use version 8 of `@tanstack/svelte-table`.
6
7
7
-
## `createTable`
8
+
The `@tanstack/svelte-table` adapter is a wrapper around the core table logic. Most of its job is related to managing state the "Svelte" way, providing types and the rendering implementation of cell/header/footer templates.
9
+
10
+
## Exports
11
+
12
+
`@tanstack/svelte-table` re-exports all of `@tanstack/table-core`'s APIs and the following:
13
+
14
+
### `createTable`
8
15
9
16
Takes an `options` object and returns a table.
10
17
11
18
```svelte
12
19
<script>
20
+
import { createTable } from '@tanstack/svelte-table'
21
+
22
+
const table = createTable({
23
+
/* ...table options... */
24
+
})
25
+
</script>
26
+
27
+
<!-- ...render your table in markup -->
28
+
```
13
29
14
-
import { createTable } from '@tanstack/svelte-table'
30
+
### FlexRender
15
31
16
-
const table = createTable(options)
32
+
A Svelte component for rendering cell/header/footer templates with dynamic values.
17
33
34
+
FlexRender supports any type of renderable content supported by Svelte:
35
+
- Scalar data types such as numbers, strings, etc.
36
+
- Svelte components (when wrapped with `renderComponent`)
37
+
38
+
Example:
39
+
40
+
```svelte
41
+
<script lang="ts">
42
+
import {
43
+
type ColumnDef,
44
+
FlexRender,
45
+
createTable,
46
+
getCoreRowModel,
47
+
renderComponent
48
+
} from '@tanstack/svelte-table'
49
+
import { StatusTag } from '$lib/components/status-tag.svelte'
50
+
import type { Person } from './types'
51
+
import { peopleData, type Person } from './people'
0 commit comments