分组状态以以下形式存储在表格中:
export type GroupingState = string[]
export type GroupingTableState = { grouping: GroupingState}表格核心内置了以下聚合函数:
sum
min
max
extent
mean
median
unique
uniqueCount
count
每个聚合函数接收以下参数:
并且应该返回一个值(通常是原始值),以构建聚合行模型。
以下是每个聚合函数的类型签名:
export type AggregationFn<TData extends AnyData> = ( getLeafRows: () => Row<TData>[], getChildRows: () => Row<TData>[]) => any可以通过将以下内容传递给 columnDefinition.aggregationFn 来使用/引用/定义聚合函数:
stringtableOptions.aggregationFns 选项提供的自定义聚合函数的 stringcolumnDefinition.aggregationFn 选项的函数columnDefinition.aggregationFn 可用的最终聚合函数列表使用以下类型:
export type AggregationFnOption<TData extends AnyData> = | 'auto' | keyof AggregationFns | BuiltInAggregationFn | AggregationFn<TData>aggregationFnaggregationFn?: AggregationFn | keyof AggregationFns | keyof BuiltInAggregationFns用于此列的聚合函数。
选项:
aggregatedCellaggregatedCell?: Renderable< { table: Table<TData> row: Row<TData> column: Column<TData> cell: Cell<TData> getValue: () => any renderValue: () => any }>如果单元格是聚合的,则显示该列的每一行的单元格。如果传递了函数,则会传递一个带有单元格上下文的 props 对象,并应返回适配器的属性类型(具体类型取决于所使用的适配器)。
enableGroupingenableGrouping?: boolean启用/禁用此列的分组。
getGroupingValuegetGroupingValue?: (row: TData) => any指定用于在此列上分组行的值。如果未指定此选项,则将使用从 accessorKey / accessorFn 派生的值。
aggregationFnaggregationFn?: AggregationFnOption<TData>列的解析聚合函数。
getCanGroupgetCanGroup: () => boolean返回列是否可以分组。
getIsGroupedgetIsGrouped: () => boolean返回列当前是否已分组。
getGroupedIndexgetGroupedIndex: () => number返回列在分组状态中的索引。
toggleGroupingtoggleGrouping: () => void切换列的分组状态。
getToggleGroupingHandlergetToggleGroupingHandler: () => () => void返回一个函数,用于切换列的分组状态。这对于传递给按钮的 onClick 属性非常有用。
getAutoAggregationFngetAutoAggregationFn: () => AggregationFn<TData> | undefined返回列的自动推断聚合函数。
getAggregationFngetAggregationFn: () => AggregationFn<TData> | undefinedgroupingColumnIdgroupingColumnId?: string如果此行已分组,则为此行分组的列的 ID。
groupingValuegroupingValue?: any如果此行已分组,则为此组中所有行的 groupingColumnId 的唯一/共享值。
getIsGroupedgetIsGrouped: () => boolean返回行当前是否已分组。
getGroupingValuegetGroupingValue: (columnId: string) => unknown返回任何行和列(包括叶子行)的分组值。
aggregationFnsaggregationFns?: Record<string, AggregationFn>此选项允许您定义自定义聚合函数,可以通过其键在列的 aggregationFn 选项中引用。
示例:
declare module '@tanstack/table-core' { interface AggregationFns { myCustomAggregation: AggregationFn<unknown> }}
const column = columnHelper.data('key', { aggregationFn: 'myCustomAggregation',})
const table = useReactTable({ columns: [column], aggregationFns: { myCustomAggregation: (columnId, leafRows, childRows) => { // 返回聚合值 }, },})manualGroupingmanualGrouping?: boolean启用手动分组。如果将此选项设置为 true,则表格将不会使用 getGroupedRowModel() 自动分组行,而是期望您在将行传递给表格之前手动分组行。如果您正在进行服务器端分组和聚合,这将非常有用。
onGroupingChangeonGroupingChange?: OnChangeFn<GroupingState>如果提供了此函数,当分组状态发生变化时将调用该函数,并且您将需要自行管理状态。您可以通过 tableOptions.state.grouping 选项将管理的状态传递回表格。
enableGroupingenableGrouping?: boolean启用/禁用所有列的分组。
getGroupedRowModelgetGroupedRowModel?: (table: Table<TData>) => () => RowModel<TData>返回在分组发生后但没有进一步处理之前的行模型。
groupedColumnModegroupedColumnMode?: false | 'reorder' | 'remove' // 默认值:`reorder`默认情况下,分组列会自动重新排序到列列表的开头。如果您希望删除它们或保留它们不变,请在此处设置适当的模式。
setGroupingsetGrouping: (updater: Updater<GroupingState>) => void设置或更新 state.grouping 状态。
resetGroupingresetGrouping: (defaultState?: boolean) => void将分组状态重置为 initialState.grouping,或者可以传递 true 强制默认空状态重置为 []。
getPreGroupedRowModelgetPreGroupedRowModel: () => RowModel<TData>返回应用任何分组之前的表格的行模型。
getGroupedRowModelgetGroupedRowModel: () => RowModel<TData>返回应用分组后的表格的行模型。