table-state
它能做什么
这项技能解释如何在 Lit 组件中保存表格设置,例如当前页,以及表格如何对它们作出响应。适合需要视图与设置保持同步的 Lit 开发者。
适用场景
- ✓当 Lit 模板必须显示当前页或其他表格设置的时候。
- ✓当你需要把组件中的表格设置传入表格的时候。
- ✓当表格设置变化后,Lit 视图没有更新的时候。
技能链接
https://github.com/TanStack/table/blob/main/packages/lit-table/skills/table-state/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“点击“下一页”后,如何更新我的 Lit 表格视图中的页码?”
每周 5 个新技能,1 分钟读完。
工作原理
它展示如何把设置保存为 Lit 响应式属性,将其传给表格,并把更新反馈回来,使组件重新渲染。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Load intent load @tanstack/table-core#table-state first for shared ownership, initialization, updater, and reset rules.
TableController selects all registered state by default and requests host updates when selected state changes. table.state is the selected render value. table.atoms.<slice>.get() and table.store.state are snapshots; those reads do not create a template subscription.
Controlled property setup
<!-- skill-snippet:check tsconfig=examples/lit/basic-table-controller/tsconfig.json -->import { LitElement, html } from 'lit'
import { customElement, state } from 'lit/decorators.js'
import {
TableController,
rowPaginationFeature,
tableFeatures,
type PaginationState,
} from '@tanstack/lit-table'
const features = tableFeatures({ rowPaginationFeature })
const columns = [{ accessorKey: 'id' }]
const data = [{ id: '1' }]
@customElement('page-status')
export class PageStatus extends LitElement {
@state() private pagination: PaginationState = { pageIndex: 0, pageSize: 10 }
private controller = new TableController<typeof features, { id: string }>(
this,
)
protected render() {
const table = this.controller.table({
features,
columns,
data,
state: { pagination: this.pagination },
onPaginationChange: (next) => {
this.pagination =
typeof next === 'function' ? next(this.pagination) : next
},
})
return html`<button @click=${() => table.nextPage()}>
Page ${table.state.pagination.pageIndex + 1}
</button>`
}
}
Reactive boundaries
- Keep one controller on the host and pass current options during render. Keep features, columns, and data references stable across host updates.
- A second
controller.table(options, selector)argument changes the shape oftable.stateand gates host updates. Keep reused selectors stable. - Use
table.subscribe(source, selector, renderCallback)for a template region that should update independently. Cache selector functions as host fields or module functions. - Feed every controlled callback result into the corresponding reactive property and pass it back through
state. Use stable external atoms throughatomswhen sharing state outside the host.
Read for the task
For advanced reactive boundaries, shared atoms, or detailed subscription examples, read reactivity. For construction or rendering setup, read getting-started.
API discovery
Inspect node_modules/@tanstack/lit-table/dist/index.d.ts, then TableController.d.ts, flexRender.d.ts, or the relevant exported declaration. Core feature APIs are in node_modules/@tanstack/table-core/dist/features/.
常见问题
为什么在处理函数中读取的值与在模板中读取的不同?
模板读取会使视图持续订阅变化,而在处理函数中读取的值只是那一刻的快照。
控制器默认选择什么?
默认选择所有已注册的表格状态,并在所选状态变化时请求宿主更新。
我可以限制模板监听的内容吗?
可以,正如指南所述,可以为模板的某个区域使用稳定的选择器。
详情
作者的原始描述
Read and control Table v9 state in lit. Use for tracked reads, subscriptions, controlled slices, and framework-specific reactive boundaries.
repo: TanStack/table
热度
- GitHub 星标
- 29k
- 排名
- #9815
- 许可证
- MIT
- 更新时间
- 2026/10/21
评论
- …
同一仓库的更多技能
getting-started
29k教你的智能体如何在 Alpine.js 中使用 TanStack Table 构建并显示数据表格,涵盖设置、响应式数据和渲染行。
table-state
29k教你的智能体如何在 Alpine.js 中读取和控制表格状态,例如当前页码,使状态变化时屏幕能够更新。
devtools
29k教你的智能体如何把 Angular 表格注册到 TanStack Devtools 面板中,使开发者在构建应用时能检查每个表格。
getting-started
29k教你的智能体如何在 Angular 中创建并显示 TanStack 表格,涵盖 injectTable 设置、模板渲染和行数据的更新。