You can keep sorting, filters, grouping or selection in your own state. The plugin changes them through the table's setters, so your handlers must take the update and show it.
'use client'; import { assistantColumn, useTableAssistant } from '@gridsay/tanstack-table/react';import { createColumnHelper, createSortedRowModel, rowSortingFeature, type SortingState, tableFeatures, useTable,} from '@tanstack/react-table';import { useState } from 'react'; type Order = { id: string; total: number };const features = tableFeatures({ rowSortingFeature, sortedRowModel: createSortedRowModel(),});const helper = createColumnHelper<typeof features, Order>();const columns = helper.columns([helper.accessor('total', { header: 'Order total' })]); export function ControlledOrders({ data }: { data: Order[] }) { const [sorting, setSorting] = useState<SortingState>([]); const table = useTable({ features, columns, data, getRowId: (row) => row.id, defaultColumn: assistantColumn, state: { sorting }, onSortingChange: setSorting, }); const { ask, undo, reset, canUndo, pending, error } = useTableAssistant(table, { url: '/api/tanstack-table', }); return ( <> <button disabled={pending} type="button" onClick={() => void ask('Largest orders first')}> Largest first </button> <button disabled={pending || !canUndo} type="button" onClick={() => undo()}>Undo</button> <button type="button" onClick={reset}>New conversation</button> {error ? <p role="alert">{error.message}</p> : null} <ol>{table.getRowModel().rows.map((row) => <li key={row.id}>{String(row.getValue('total'))}</li>)}</ol> </> );}This table registers sorting only, so filter and group questions are declined. It posts to the route from the quickstart.
After it writes an answer, the plugin waits up to one second for the table's state to show it. Update the state at once; if your rows come from a server, fetch after.
ask fails with cannot_apply, and the plugin puts the earlier state back.ask fails with cannot_apply too.The plugin can put the earlier state back only through your setters. A handler that ignores every write keeps whatever it holds.
| State | When it changes |
|---|---|
columnFilters | Replaced when filtering is on, including filters set by hand. |
globalFilter | Cleared when filtering is on and the table has a global filter. |
sorting | Replaced when sorting is on. |
grouping | Replaced when grouping is on. |
rowSelection | Replaced when the answer selects rows; otherwise left alone. |
pagination, expanded | Not written by an answer. TanStack's auto-reset settings may change them; undo puts them back. |
Column visibility, order, pinning and sizes never change.
Each question sends the current sorting and grouping, including a header click, and the filters an earlier answer set. Questions go one at a time, so a follow-up reads the table after the previous answer is on it.
Keep one hook per table. The conversation and the answer to undo last as long as the component stays mounted. The hook reads the latest table and options for every question, so a new getToken or url takes effect on the next one.