跳到正文
Skillpedia
网站与界面(前端)29k · #9806

table-state

它能做什么

这项技能解释如何跟踪表格的设置,例如当前显示的是哪一页,以及如何在 Alpine.js 页面中更新它们。适合需要表格与页面其他部分保持同步的开发者。

适用场景

  • ✓当表格的当前页或其他设置需要显示在按钮或标签中的时候。
  • ✓当你想从自己的代码中控制表格状态的一部分的时候。
  • ✓当状态变化后表格没有在屏幕上更新的时候。

技能链接

https://github.com/TanStack/table/blob/main/packages/alpine-table/skills/table-state/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/TanStack/table/blob/main/packages/alpine-table/skills/table-state/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我的 Alpine.js 表格在点击下一页后,页码没有更新。我该如何解决?”

每周 5 个新技能,1 分钟读完。

工作原理

它展示如何通过 getter 和回调传递受控状态,使 Alpine 能读取最新的值,并在其变化时更新屏幕。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Load intent load @tanstack/table-core#table-state first for shared ownership, initialization, updater, and reset rules.

Alpine's table proxy makes reads inside x-text, x-for, x-if, bound attributes, x-effect, and Alpine getters reactive. Event handlers read the current value when invoked. A captured value outside a binding does not become a continuing subscription, and there is no table.Subscribe component.

Controlled getter setup

<!-- skill-snippet:check -->
import Alpine from 'alpinejs'
import {
  createTable,
  rowPaginationFeature,
  tableFeatures,
  type PaginationState,
} from '@tanstack/alpine-table'

const features = tableFeatures({ rowPaginationFeature })
const columns = [{ accessorKey: 'name' }]
const data = [{ name: 'Ada' }]

Alpine.data('pagedTable', () => {
  const local = Alpine.reactive<{ pagination: PaginationState }>({
    pagination: { pageIndex: 0, pageSize: 10 },
  })
  const table = createTable({
    features,
    columns,
    data,
    state: {
      get pagination() {
        return local.pagination
      },
    },
    onPaginationChange: (next) => {
      local.pagination =
        typeof next === 'function' ? next(local.pagination) : next
    },
  })
  return { table }
})

Read table.atoms.pagination.get().pageIndex directly in an Alpine binding.

Reactive boundaries

  • A controlled slice needs a getter and callback. state: { pagination: local.pagination } captures the old object when the owner later replaces it.
  • Expose changing data through get data() and keep features, columns, and derived data stable outside reevaluated getters.
  • The proxy normally reevaluates all Table-reading bindings for every state change. A narrow atom read does not by itself narrow that broad invalidation.
  • For shared state, pass a stable external atom through atoms; omit controlled state and its callback for the same slice.

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/alpine-table/dist/index.d.ts, createTable.d.ts, and reactivity.d.ts. Core feature APIs are in node_modules/@tanstack/table-core/dist/features/.

常见问题

为什么表格状态变化时不刷新?

通常是因为数据或状态只被复制了一次,而不是通过 getter 读取,因此 Alpine 无法看到之后的变化。

我可以在点击处理函数中一次性读取一个值吗?

可以,事件处理函数在运行的那一刻会读取当前的值。

这会取代核心表格指南吗?

不会,它在核心指南中共享的所有权和重置规则之上,增加了 Alpine 特有的规则。

详情

作者的原始描述

Read and control Table v9 state in alpine. Use for tracked reads, subscriptions, controlled slices, and framework-specific reactive boundaries.

repo: TanStack/table

热度

GitHub 星标
29k
排名
#9806
许可证
MIT
更新时间
2026/10/21

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能