TanStack Table 具有一个简单的底层内部状态管理系统,用于存储和管理表格的状态。它还允许您选择性地提取任何您需要在自己的状态管理中管理的状态。本指南将引导您了解与表格状态交互和管理的不同方式。
您无需进行任何特殊设置即可使表格状态正常工作。如果您在 state、initialState 或任何 on[State]Change 表格选项中未传递任何内容,则表格将在内部管理其自身的状态。您可以使用 table.getState() 表格实例 API 访问此内部状态的任何部分。
private tableController = new TableController<Person>(this);
render() {
const table = this.tableController.table({
columns,
data,
...
})
console.log(table.getState()) //access the entire internal state
console.log(table.getState().rowSelection) //access just the row selection state
// ...
}
private tableController = new TableController<Person>(this);
render() {
const table = this.tableController.table({
columns,
data,
...
})
console.log(table.getState()) //access the entire internal state
console.log(table.getState().rowSelection) //access just the row selection state
// ...
}
如果您需要对某些状态执行的操作只是自定义其初始默认值,则仍然无需自己管理任何状态。您只需在表格实例的 initialState 选项中设置值即可。
render() {
const table = this.tableController.table({
columns,
data,
initialState: {
columnOrder: ['age', 'firstName', 'lastName'], //customize the initial column order
columnVisibility: {
id: false //hide the id column by default
},
expanded: true, //expand all rows by default
sorting: [
{
id: 'age',
desc: true //sort by age in descending order by default
}
]
},
})
return html`...`;
}
render() {
const table = this.tableController.table({
columns,
data,
initialState: {
columnOrder: ['age', 'firstName', 'lastName'], //customize the initial column order
columnVisibility: {
id: false //hide the id column by default
},
expanded: true, //expand all rows by default
sorting: [
{
id: 'age',
desc: true //sort by age in descending order by default
}
]
},
})
return html`...`;
}
注意:仅在 initialState 或 state 中指定每个特定状态,但不要两者都指定。如果您将特定状态值同时传递给 initialState 和 state,则 state 中的初始化状态将覆盖 initialState 中的任何对应值。
如果您需要在应用程序的其他区域轻松访问表格状态,TanStack Table 可以轻松地在您自己的状态管理系统中控制和管理任何或所有表格状态。您可以通过将您自己的状态和状态管理函数传递给 state 和 on[State]Change 表格选项来做到这一点。
您可以仅控制您需要轻松访问的状态。如果您不需要,则**不必**控制所有表格状态。建议仅根据具体情况控制您所需的状态。
为了控制特定状态,您需要同时将相应的 state 值和 on[State]Change 函数传递给表格实例。
让我们以筛选、排序和分页为例,在“手动”服务器端数据获取场景中。您可以将筛选、排序和分页状态存储在您自己的状态管理中,但如果您的 API 不关心这些值,则可以忽略任何其他状态,例如列顺序、列可见性等。
import {html} from "lit";
@customElement('my-component')
class MyComponent extends LitElement {
@state()
private _sorting: SortingState = []
render() {
const table = this.tableController.table({
columns,
data,
state: {
sorting: this._sorting,
},
onSortingChange: updaterOrValue => {
if (typeof updaterOrValue === 'function') {
this._sorting = updaterOrValue(this._sorting)
} else {
this._sorting = updaterOrValue
}
},
getSortedRowModel: getSortedRowModel(),
getCoreRowModel: getCoreRowModel(),
})
return html`...`
}
}
//...
import {html} from "lit";
@customElement('my-component')
class MyComponent extends LitElement {
@state()
private _sorting: SortingState = []
render() {
const table = this.tableController.table({
columns,
data,
state: {
sorting: this._sorting,
},
onSortingChange: updaterOrValue => {
if (typeof updaterOrValue === 'function') {
this._sorting = updaterOrValue(this._sorting)
} else {
this._sorting = updaterOrValue
}
},
getSortedRowModel: getSortedRowModel(),
getCoreRowModel: getCoreRowModel(),
})
return html`...`
}
}
//...
或者,您可以使用 onStateChange 表格选项控制整个表格状态。它会将整个表格状态提升到您自己的状态管理系统中。请注意这种方法,因为您可能会发现将某些频繁更改的状态值(如 columnSizingInfo 状态)提升到组件树中可能会导致不良的性能问题。
可能需要更多技巧才能使其工作。如果您使用 onStateChange 表格选项,则 state 的初始值必须填充您要使用的所有功能的所有相关状态值。您可以手动键入所有初始状态值,或者以特殊方式使用 table.setOptions API,如下所示。
private tableController = new TableController<Person>(this);
@state()
private _tableState;
render() {
const table = this.tableController.table({
columns,
data,
getCoreRowModel: getCoreRowModel(),
getSortedRowModel: getSortedRowModel()
})
const state = { ...table.initialState, ...this._tableState };
table.setOptions(prev => ({
...prev,
state,
onStateChange: updater => {
this._tableState =
updater instanceof Function ? updater(state) : updater //any state changes will be pushed up to our own state management
},
}))
return html`...`;
}
private tableController = new TableController<Person>(this);
@state()
private _tableState;
render() {
const table = this.tableController.table({
columns,
data,
getCoreRowModel: getCoreRowModel(),
getSortedRowModel: getSortedRowModel()
})
const state = { ...table.initialState, ...this._tableState };
table.setOptions(prev => ({
...prev,
state,
onStateChange: updater => {
this._tableState =
updater instanceof Function ? updater(state) : updater //any state changes will be pushed up to our own state management
},
}))
return html`...`;
}
到目前为止,我们已经看到 on[State]Change 和 onStateChange 表格选项可以“提升”表格状态更改到我们自己的状态管理中。但是,关于使用这些选项,您应该注意一些事项。
指定 on[State]Change 回调会告诉表格实例,这将是一个受控状态。如果您未指定相应的 state 值,则该状态将使用其初始值“冻结”。
@state()
private _sorting = [];
//...
render() {
const table = this.tableController.table({
columns,
data,
state: {
sorting: this._sorting,
},
onSortingChange: updaterOrValue => {
if (typeof updaterOrValue === 'function') {
this._sorting = updaterOrValue(this._sorting)
} else {
this._sorting = updaterOrValue
}
},
getSortedRowModel: getSortedRowModel(),
getCoreRowModel: getCoreRowModel(),
})
return html`...`;
}
@state()
private _sorting = [];
//...
render() {
const table = this.tableController.table({
columns,
data,
state: {
sorting: this._sorting,
},
onSortingChange: updaterOrValue => {
if (typeof updaterOrValue === 'function') {
this._sorting = updaterOrValue(this._sorting)
} else {
this._sorting = updaterOrValue
}
},
getSortedRowModel: getSortedRowModel(),
getCoreRowModel: getCoreRowModel(),
})
return html`...`;
}
on[State]Change 和 onStateChange 回调的工作方式与 React 中的 setState 函数完全相同。更新器值可以是新的状态值,也可以是接受先前的状态值并返回新的状态值的回调函数。
这有什么含义?这意味着,如果您想在任何 on[State]Change 回调中添加一些额外的逻辑,您可以这样做,但是您需要检查新的传入更新器值是函数还是值。
这就是为什么您会在上面的示例中看到 updater instanceof Function ? updater(state.value) : updater 模式的原因。此模式检查更新器是否为函数,如果是,则使用先前的状态值调用该函数以获取新的状态值。
TanStack Table 中的所有复杂状态都有自己的 TypeScript 类型,您可以导入和使用。这对于确保您为要控制的状态值使用正确的数据结构和属性非常方便。
import { TableController, type SortingState } from '@tanstack/lit-table'
//...
@state()
private _sorting: SortingState = [
{
id: 'age', //you should get autocomplete for the `id` and `desc` properties
desc: true,
}
]
import { TableController, type SortingState } from '@tanstack/lit-table'
//...
@state()
private _sorting: SortingState = [
{
id: 'age', //you should get autocomplete for the `id` and `desc` properties
desc: true,
}
]
您的每周 JavaScript 新闻。每周一免费发送给超过 100,000 名开发人员。