@tanstack/vue-table 9.0.0-beta.40 → 9.0.0-beta.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,127 @@
1
+ ---
2
+ name: with-tanstack-query
3
+ description: >
4
+ Compose reactive Vue Query keys and results with Vue Table manual row processing, refs/computed state, server counts, and already-processed pages without duplicating query data into a drifting local ref.
5
+ metadata:
6
+ type: composition
7
+ library: '@tanstack/vue-table'
8
+ framework: vue
9
+ library_version: '9.0.0-beta.43'
10
+ requires:
11
+ - '@tanstack/table-core#client-vs-server'
12
+ - getting-started
13
+ - table-state
14
+ sources:
15
+ - 'TanStack/table:examples/vue/with-tanstack-query'
16
+ - 'TanStack/table:docs/framework/vue/guide/pagination.md'
17
+ ---
18
+
19
+ This skill builds on `@tanstack/table-core#client-vs-server`, `getting-started`, and `table-state`. Name each client- and server-owned processing stage first.
20
+
21
+ ## Setup
22
+
23
+ ```ts
24
+ import { computed, ref } from 'vue'
25
+ import { keepPreviousData, useQuery } from '@tanstack/vue-query'
26
+ import {
27
+ rowPaginationFeature,
28
+ tableFeatures,
29
+ useTable,
30
+ } from '@tanstack/vue-table'
31
+
32
+ const pagination = ref({ pageIndex: 0, pageSize: 20 })
33
+ const query = useQuery(() => ({
34
+ queryKey: ['people', pagination.value.pageIndex, pagination.value.pageSize],
35
+ queryFn: () =>
36
+ fetch(
37
+ `/api/people?page=${pagination.value.pageIndex}&size=${pagination.value.pageSize}`,
38
+ ).then((r) => r.json()),
39
+ placeholderData: keepPreviousData,
40
+ }))
41
+ const data = computed(() => query.data.value?.rows ?? [])
42
+ const rowCount = computed(() => query.data.value?.rowCount ?? 0)
43
+ const state = computed(() => ({ pagination: pagination.value }))
44
+ const table = useTable({
45
+ features: tableFeatures({ rowPaginationFeature }),
46
+ columns,
47
+ data,
48
+ rowCount,
49
+ manualPagination: true,
50
+ state,
51
+ onPaginationChange: (next) => {
52
+ pagination.value =
53
+ typeof next === 'function' ? next(pagination.value) : next
54
+ },
55
+ })
56
+ ```
57
+
58
+ ## Core Patterns
59
+
60
+ ### Keep query dependencies reactive
61
+
62
+ Use the Vue Query options function and read refs inside it. Include every manual filter/sort/page input in the query key.
63
+
64
+ ### Pass Query results directly
65
+
66
+ Expose result fields as computed refs. Introduce a second local data ref only for an explicit editing workflow with a cache-write policy.
67
+
68
+ ## Common Mistakes
69
+
70
+ ### HIGH Unwrapping before query construction
71
+
72
+ Wrong:
73
+
74
+ ```ts
75
+ const page = pagination.value.pageIndex
76
+ useQuery(() => ({ queryKey: ['people', page], queryFn }))
77
+ ```
78
+
79
+ Correct:
80
+
81
+ ```ts
82
+ useQuery(() => ({ queryKey: ['people', pagination.value.pageIndex], queryFn }))
83
+ ```
84
+
85
+ Only reads inside the reactive options function become query dependencies.
86
+
87
+ Source: `examples/vue/with-tanstack-query/src/App.tsx`
88
+
89
+ ### HIGH Mirroring Query data locally
90
+
91
+ Wrong:
92
+
93
+ ```ts
94
+ const rows = ref(query.data.value?.rows ?? [])
95
+ ```
96
+
97
+ Correct:
98
+
99
+ ```ts
100
+ const rows = computed(() => query.data.value?.rows ?? [])
101
+ ```
102
+
103
+ A one-time copy drifts from subsequent cache results.
104
+
105
+ Source: `examples/vue/with-tanstack-query/src/App.tsx`
106
+
107
+ ### HIGH Omitting server counts
108
+
109
+ Wrong:
110
+
111
+ ```ts
112
+ useTable({ features, columns, data, manualPagination: true })
113
+ ```
114
+
115
+ Correct:
116
+
117
+ ```ts
118
+ useTable({ features, columns, data, rowCount, manualPagination: true })
119
+ ```
120
+
121
+ One returned page cannot tell Table how many pages the server has.
122
+
123
+ Source: `docs/framework/vue/guide/pagination.md`
124
+
125
+ ## API Discovery
126
+
127
+ Inspect installed `@tanstack/vue-table/src/useTable.ts`, installed `@tanstack/vue-query/src`, and the relevant manual Table feature source for exact option types.
@@ -0,0 +1,116 @@
1
+ ---
2
+ name: with-tanstack-virtual
3
+ description: >
4
+ Virtualize Vue Table final row or column models with reactive counts and scroll targets, stable keys, measurement, spacer geometry, sticky CSS, grid/flex widths, and infinite server data.
5
+ metadata:
6
+ type: composition
7
+ library: '@tanstack/vue-table'
8
+ framework: vue
9
+ library_version: '9.0.0-beta.43'
10
+ requires:
11
+ - '@tanstack/table-core#core'
12
+ - getting-started
13
+ - table-state
14
+ sources:
15
+ - 'TanStack/table:docs/framework/vue/guide/virtualization.md'
16
+ - 'TanStack/table:examples/vue/virtualized-rows'
17
+ - 'TanStack/table:examples/vue/virtualized-columns'
18
+ - 'TanStack/table:examples/vue/virtualized-infinite-scrolling'
19
+ ---
20
+
21
+ This skill builds on `@tanstack/table-core#core`, `getting-started`, and `table-state`. Virtual consumes final Table models; it is not registered in `tableFeatures`.
22
+
23
+ ## Setup
24
+
25
+ ```ts
26
+ import { computed, ref } from 'vue'
27
+ import { useVirtualizer } from '@tanstack/vue-virtual'
28
+
29
+ const scrollElement = ref<HTMLElement | null>(null)
30
+ const rows = computed(() => table.getRowModel().rows)
31
+ const rowVirtualizer = useVirtualizer(
32
+ computed(() => ({
33
+ count: rows.value.length,
34
+ getScrollElement: () => scrollElement.value,
35
+ estimateSize: () => 34,
36
+ getItemKey: (index) => rows.value[index]!.id,
37
+ overscan: 5,
38
+ })),
39
+ )
40
+ const virtualRows = computed(() => rowVirtualizer.value.getVirtualItems())
41
+ const totalSize = computed(() => rowVirtualizer.value.getTotalSize())
42
+ ```
43
+
44
+ ## Core Patterns
45
+
46
+ ### Derive from current visible models
47
+
48
+ Rows come from `table.getRowModel().rows`; columns come from `table.getVisibleLeafColumns()`. Use computed options so counts and scroll targets update.
49
+
50
+ ### Implement the geometry
51
+
52
+ Give the scroll container a bounded height and positioning context, create a spacer using `getTotalSize()`, and translate/measure virtual items. Follow the grid/flex examples for dynamic row heights and sticky headers.
53
+
54
+ ### Coordinate infinite fetching
55
+
56
+ Fetch near the last virtual item only while `totalFetched < serverRowCount` and no request is active. Manual sorting means the server must return the sorted order and a sort change normally resets pages.
57
+
58
+ ## Common Mistakes
59
+
60
+ ### HIGH Passing a plain options snapshot
61
+
62
+ Wrong:
63
+
64
+ ```ts
65
+ useVirtualizer({ count: rows.value.length, getScrollElement })
66
+ ```
67
+
68
+ Correct:
69
+
70
+ ```ts
71
+ useVirtualizer(computed(() => ({ count: rows.value.length, getScrollElement })))
72
+ ```
73
+
74
+ Computed options keep the virtual range synchronized with Vue’s current model.
75
+
76
+ Source: `examples/vue/virtualized-rows/src/App.vue`
77
+
78
+ ### HIGH Virtualizing source arrays
79
+
80
+ Wrong:
81
+
82
+ ```ts
83
+ const rows = computed(() => data.value)
84
+ ```
85
+
86
+ Correct:
87
+
88
+ ```ts
89
+ const rows = computed(() => table.getRowModel().rows)
90
+ ```
91
+
92
+ Source arrays do not represent Table’s current filtering, sorting, expansion, or pagination.
93
+
94
+ Source: `docs/framework/vue/guide/virtualization.md`
95
+
96
+ ### HIGH Assuming Virtual provides CSS
97
+
98
+ Wrong:
99
+
100
+ ```vue
101
+ <div v-for="item in virtualRows" :key="item.key">{{ rows[item.index].id }}</div>
102
+ ```
103
+
104
+ Correct:
105
+
106
+ ```vue
107
+ <div :style="{ height: `${totalSize}px`, position: 'relative' }"><div style="position:absolute"></div></div>
108
+ ```
109
+
110
+ Virtual provides measurements, not spacer layout, transforms, sticky positioning, or Table column widths.
111
+
112
+ Source: `examples/vue/virtualized-columns/src/App.vue`
113
+
114
+ ## API Discovery
115
+
116
+ Inspect installed `@tanstack/vue-table/src` and `@tanstack/vue-virtual/src`; use the maintained Vue examples for exact row, column, and infinite layout combinations.
@@ -1,360 +0,0 @@
1
- ---
2
- name: vue/client-to-server
3
- description: >
4
- Convert a client-side `@tanstack/vue-table` to server-side. Set `manualPagination` /
5
- `manualSorting` / `manualFiltering` / `manualGrouping` / `manualExpanding` for whichever
6
- slices the server owns; omit the matching row model factory from `tableFeatures` (don't ship
7
- `paginatedRowModel` when the server paginates); supply `rowCount` so `table.getPageCount()`
8
- works; own the relevant state slices via either external atoms (`@tanstack/vue-store`
9
- `createAtom` + `options.atoms`) or `state` + `on[State]Change` with getter wrappers. Key any
10
- data fetch (TanStack Query / fetch / rxResource alternative) on the controlled state and use
11
- `placeholderData: keepPreviousData` (or equivalent) to avoid a 0-rows flash between pages.
12
- type: lifecycle
13
- library: tanstack-table
14
- framework: vue
15
- library_version: '9.0.0-alpha.48'
16
- requires:
17
- - state-management
18
- - pagination
19
- - filtering
20
- - sorting
21
- sources:
22
- - examples/vue/basic-external-atoms/
23
- - examples/vue/basic-external-state/
24
- - examples/vue/with-tanstack-query/
25
- - docs/framework/vue/guide/table-state.md
26
- ---
27
-
28
- # Client-to-Server Conversion (Vue)
29
-
30
- ## Dependencies
31
-
32
- ```bash
33
- pnpm add @tanstack/vue-table @tanstack/vue-store
34
- # Recommended fetch layer:
35
- pnpm add @tanstack/vue-query
36
- ```
37
-
38
- External atoms (`@tanstack/vue-store`) are recommended for server-managed slices. They cut the
39
- `on[State]Change` plumbing entirely — the table writes to the atom; the query keys on the atom.
40
-
41
- ## Setup — minimal server-paginated table
42
-
43
- ```vue
44
- <script setup lang="ts">
45
- import { computed, ref, watchEffect } from 'vue'
46
- import { createAtom, useSelector } from '@tanstack/vue-store'
47
- import { keepPreviousData, useQuery } from '@tanstack/vue-query'
48
- import {
49
- FlexRender,
50
- createColumnHelper,
51
- rowPaginationFeature,
52
- tableFeatures,
53
- useTable,
54
- type PaginationState,
55
- } from '@tanstack/vue-table'
56
- import { fetchPeople } from './api'
57
-
58
- type Person = { firstName: string; lastName: string; age: number }
59
-
60
- const features = tableFeatures({ rowPaginationFeature })
61
- const columnHelper = createColumnHelper<typeof features, Person>()
62
- const columns = columnHelper.columns([
63
- columnHelper.accessor('firstName', { header: 'First' }),
64
- columnHelper.accessor('lastName', { header: 'Last' }),
65
- columnHelper.accessor('age', { header: 'Age' }),
66
- ])
67
-
68
- // 1) Own pagination in an external atom. Cheap for the table to write through.
69
- const paginationAtom = createAtom<PaginationState>({
70
- pageIndex: 0,
71
- pageSize: 10,
72
- })
73
- const pagination = useSelector(paginationAtom)
74
-
75
- // 2) Key the query on the atom value. table.setPageIndex(...) writes to the atom
76
- // → useSelector re-evaluates → useQuery refetches.
77
- const dataQuery = useQuery(() => ({
78
- queryKey: ['people', pagination.value],
79
- queryFn: () => fetchPeople(pagination.value),
80
- placeholderData: keepPreviousData, // avoid "0 rows" flash between pages
81
- }))
82
-
83
- const tableData = computed<Person[]>(() => dataQuery.data.value?.rows ?? [])
84
- const rowCount = ref(0)
85
- watchEffect(() => {
86
- const next = dataQuery.data.value?.rowCount
87
- if (next != null) rowCount.value = next // keep last known total for pager UI
88
- })
89
-
90
- // 3) Manual pagination + `rowCount`. No paginatedRowModel in features — server paginates.
91
- const table = useTable({
92
- features,
93
- columns,
94
- data: tableData,
95
- rowCount,
96
- atoms: { pagination: paginationAtom },
97
- manualPagination: true,
98
- })
99
- </script>
100
- ```
101
-
102
- Source: `examples/vue/with-tanstack-query/src/App.tsx`, `examples/vue/basic-external-atoms/`.
103
-
104
- ## Core Patterns
105
-
106
- ### 1. The `manual*` flag + factory omission pair
107
-
108
- Pick which slices live server-side and flip the matching `manual*` flag. **Also omit the
109
- matching row model factory from `tableFeatures`** — otherwise the table re-processes
110
- server-processed rows.
111
-
112
- | Server owns | Set | Omit from `tableFeatures` |
113
- | ----------- | ------------------------ | ------------------------- |
114
- | Pagination | `manualPagination: true` | `paginatedRowModel` |
115
- | Sorting | `manualSorting: true` | `sortedRowModel` |
116
- | Filtering | `manualFiltering: true` | `filteredRowModel` |
117
- | Grouping | `manualGrouping: true` | `groupedRowModel` |
118
- | Expanding | `manualExpanding: true` | `expandedRowModel` |
119
-
120
- Column visibility / ordering / pinning / row selection are client-side state and stay as-is.
121
-
122
- ### 2. `rowCount` so `getPageCount()` works
123
-
124
- Without `rowCount`, `getPageCount()` falls back to `Math.ceil(data.length / pageSize)` — i.e.
125
- `1` if the server already paginated. The pager locks at "Page 1 of 1".
126
-
127
- ```ts
128
- useTable({
129
- features,
130
- columns,
131
- data: tableData,
132
- rowCount: dataQuery.data.value?.rowCount, // or a stable ref/computed
133
- atoms: { pagination: paginationAtom },
134
- manualPagination: true,
135
- })
136
- ```
137
-
138
- If `rowCount` isn't immediately available, hold the last known value in a `ref` and update via
139
- `watchEffect` so the pager doesn't reset to 0 during refetches.
140
-
141
- ### 3. Two state-ownership shapes (pick one per slice)
142
-
143
- **External atoms (recommended with Query).** Table writes through to the atom. No
144
- `on[State]Change` needed.
145
-
146
- ```ts
147
- const paginationAtom = createAtom<PaginationState>({
148
- pageIndex: 0,
149
- pageSize: 10,
150
- })
151
- useTable({
152
- features,
153
- columns,
154
- data,
155
- rowCount,
156
- atoms: { pagination: paginationAtom },
157
- manualPagination: true,
158
- })
159
- ```
160
-
161
- **Classic `state` + `on[State]Change` with getters.** Required when migrating from v8 or
162
- integrating with existing Vue ref-based state. Each slice must be a getter so Vue tracks
163
- `.value`.
164
-
165
- ```ts
166
- const pagination = ref<PaginationState>({ pageIndex: 0, pageSize: 10 })
167
-
168
- useTable({
169
- features,
170
- columns,
171
- data,
172
- rowCount,
173
- state: {
174
- get pagination() {
175
- return pagination.value
176
- },
177
- },
178
- onPaginationChange: (u) => {
179
- pagination.value = typeof u === 'function' ? u(pagination.value) : u
180
- },
181
- manualPagination: true,
182
- })
183
- ```
184
-
185
- **Precedence:** `atoms[slice]` > `state[slice]` > internal `baseAtoms[slice]`. Don't pass the
186
- same slice through both — the atoms wins silently.
187
-
188
- ### 4. Cache keys must include controlled state
189
-
190
- ```ts
191
- const sortingAtom = createAtom<SortingState>([])
192
- const paginationAtom = createAtom<PaginationState>({
193
- pageIndex: 0,
194
- pageSize: 10,
195
- })
196
- const sorting = useSelector(sortingAtom)
197
- const pagination = useSelector(paginationAtom)
198
-
199
- const dataQuery = useQuery(() => ({
200
- queryKey: [
201
- 'people',
202
- { sorting: sorting.value, pagination: pagination.value },
203
- ],
204
- queryFn: () =>
205
- fetchPeople({ sorting: sorting.value, pagination: pagination.value }),
206
- placeholderData: keepPreviousData,
207
- }))
208
- ```
209
-
210
- If pagination/sort/filter aren't in `queryKey`, Query won't refetch when the user clicks a
211
- pager button — the buttons "do nothing" from the user's POV.
212
-
213
- ### 5. Mixed client + server features still work
214
-
215
- Column visibility, ordering, pinning, and row selection are client state — they don't depend
216
- on the row model and continue to function with `manualPagination`/`manualSorting`/`manualFiltering`.
217
- You can have a server-paginated table where the user pins or hides columns locally.
218
-
219
- ## Common Mistakes
220
-
221
- ### Forgetting `manualPagination` / `manualSorting` / `manualFiltering` (CRITICAL)
222
-
223
- The table double-processes server-processed rows. If the server returned page 2 of 50,
224
- the table will paginate that 10-row slice again and show "Page 1 of 1".
225
-
226
- ```ts
227
- // ❌
228
- useTable({
229
- features,
230
- columns,
231
- data: serverPage.rows,
232
- rowCount,
233
- atoms: { pagination: paginationAtom },
234
- // missing: manualPagination: true
235
- })
236
- ```
237
-
238
- ### Leaving `paginatedRowModel` registered when server paginates (CRITICAL)
239
-
240
- ```ts
241
- // ❌ Factory ships for nothing AND the table re-paginates server-sliced data.
242
- const features = tableFeatures({
243
- rowPaginationFeature,
244
- paginatedRowModel: createPaginatedRowModel(),
245
- })
246
-
247
- // ✅ Omit the factory when the server paginates; keep the feature for its API.
248
- const features = tableFeatures({ rowPaginationFeature })
249
- ```
250
-
251
- Same applies to `sortedRowModel`, `filteredRowModel`, `groupedRowModel`, `expandedRowModel`
252
- when the server owns the slice.
253
-
254
- ### Omitting `rowCount` (CRITICAL)
255
-
256
- `getPageCount()` returns `1` if the server already paginated. The pager UI locks at
257
- "Page 1 of 1" and users can't navigate.
258
-
259
- ### Passing `state.pagination` without `onPaginationChange` (CRITICAL)
260
-
261
- ```ts
262
- // ❌ table.setPageIndex(2) is a no-op — no writeback handler.
263
- const pagination = ref({ pageIndex: 0, pageSize: 10 })
264
- useTable({
265
- features,
266
- columns,
267
- data,
268
- rowCount,
269
- state: {
270
- get pagination() {
271
- return pagination.value
272
- },
273
- },
274
- manualPagination: true,
275
- })
276
-
277
- // ✅ Either pair `state` with `on[State]Change`, OR use `atoms`.
278
- ```
279
-
280
- ### Mixing `state.pagination` AND `atoms.pagination` for the same slice (HIGH)
281
-
282
- ```ts
283
- useTable({
284
- // ...
285
- state: {
286
- get pagination() {
287
- return localPagination.value
288
- },
289
- }, // silently ignored
290
- onPaginationChange: setLocalPagination, // silently ignored
291
- atoms: { pagination: paginationAtom }, // wins
292
- })
293
- ```
294
-
295
- Atoms beat `state`; the `state` plumbing is dead but lingering in the code. Pick one mechanism.
296
-
297
- ### Forgetting to include controlled state in `queryKey` (CRITICAL)
298
-
299
- ```ts
300
- // ❌ Never refetches when pagination changes.
301
- useQuery(() => ({
302
- queryKey: ['people'],
303
- queryFn: () => fetchPeople(pagination.value),
304
- }))
305
-
306
- // ✅
307
- useQuery(() => ({
308
- queryKey: ['people', pagination.value],
309
- queryFn: () => fetchPeople(pagination.value),
310
- }))
311
- ```
312
-
313
- ### Skipping `placeholderData: keepPreviousData` (HIGH)
314
-
315
- Between fetches the table collapses to 0 rows, the row container collapses, and scroll
316
- position jumps. `keepPreviousData` keeps the previous page visible during the refetch.
317
-
318
- ### Passing a raw `ref` to `state.pagination` without a getter (CRITICAL — Vue-specific)
319
-
320
- ```ts
321
- // ❌ Vue can't track .value changes on the captured ref object.
322
- state: { pagination: pagination }
323
-
324
- // ✅
325
- state: { get pagination() { return pagination.value } }
326
- ```
327
-
328
- ### Hand-rolling sort/page state instead of using the API (CRITICAL — #1 AI tell)
329
-
330
- ```ts
331
- // ❌ Manual state machine.
332
- const pageIndex = ref(0)
333
- const next = () => {
334
- pageIndex.value++
335
- refetch()
336
- }
337
-
338
- // ✅ Built-ins.
339
- table.nextPage()
340
- table.setPageIndex(0)
341
- table.setSorting([{ id: 'age', desc: true }])
342
- table.setColumnFilters(/* ... */)
343
- ```
344
-
345
- ### "API missing" because the feature isn't in `features` (CRITICAL — v9-specific)
346
-
347
- Server-side pagination still needs `rowPaginationFeature` in `tableFeatures({...})` — that's
348
- what surfaces `table.setPageIndex`, `table.nextPage`, `table.getPageCount`. The row model
349
- factory (`paginatedRowModel`) is what you omit; the feature stays.
350
-
351
- ```ts
352
- const features = tableFeatures({ rowPaginationFeature }) // ✅ even with manualPagination
353
- ```
354
-
355
- ## See Also
356
-
357
- - `tanstack-table/vue/compose-with-tanstack-query` — the Query-specific wiring
358
- - `tanstack-table/vue/compose-with-tanstack-store` — external atoms in depth
359
- - `tanstack-table/vue/table-state` — getter rule, atom precedence
360
- - `tanstack-table/table-core/pagination` — `manualPagination` / `rowCount` semantics