Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat get grouping value #4833

Merged
merged 2 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/api/features/grouping.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ enableGrouping?: boolean

Enables/disables grouping for this column.

### `getGroupingValue`

```tsx
getGroupingValue?: (row: TData) => any
```

Specify a value to be used for grouping rows on this column. If this option is not specified, the value derived from `accessorKey` / `accessorFn` will be used instead.

## Column API

### `aggregationFn`
Expand Down Expand Up @@ -202,6 +210,14 @@ getIsGrouped: () => boolean

Returns whether or not the row is currently grouped.

### `getGroupingValue`

```tsx
getGroupingValue: (columnId: string) => unknown
```

Returns the grouping value for any row and column (including leaf rows).

## Table Options

### `aggregationFns`
Expand Down
5 changes: 5 additions & 0 deletions examples/react/grouping/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ function App() {
accessorKey: 'firstName',
header: 'First Name',
cell: info => info.getValue(),
/**
* override the value used for row grouping
* (otherwise, defaults to the value derived from accessorKey / accessorFn)
*/
getGroupingValue: row => `${row.firstName} ${row.lastName}`,
},
{
accessorFn: row => row.lastName,
Expand Down
24 changes: 23 additions & 1 deletion packages/table-core/src/features/Grouping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface GroupingColumnDef<TData extends RowData, TValue> {
ReturnType<Cell<TData, TValue>['getContext']>
>
enableGrouping?: boolean
getGroupingValue?: (row: TData) => any
}

export interface GroupingColumn<TData extends RowData> {
Expand All @@ -56,6 +57,7 @@ export interface GroupingRow {
groupingColumnId?: string
groupingValue?: unknown
getIsGrouped: () => boolean
getGroupingValue: (columnId: string) => unknown
_groupingValuesCache: Record<string, any>
}

Expand Down Expand Up @@ -229,9 +231,29 @@ export const Grouping: TableFeature = {
}
},

createRow: <TData extends RowData>(row: Row<TData>): GroupingRow => {
createRow: <TData extends RowData>(
row: Row<TData>,
table: Table<TData>
): GroupingRow => {
return {
getIsGrouped: () => !!row.groupingColumnId,
getGroupingValue: columnId => {
if (row._groupingValuesCache.hasOwnProperty(columnId)) {
return row._groupingValuesCache[columnId]
}

const column = table.getColumn(columnId)

if (!column?.columnDef.getGroupingValue) {
return row.getValue(columnId)
}

row._groupingValuesCache[columnId] = column.columnDef.getGroupingValue(
row.original
)

return row._groupingValuesCache[columnId]
},
_groupingValuesCache: {},
}
},
Expand Down
2 changes: 1 addition & 1 deletion packages/table-core/src/utils/getGroupedRowModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ function groupBy<TData extends RowData>(rows: Row<TData>[], columnId: string) {
const groupMap = new Map<any, Row<TData>[]>()

return rows.reduce((map, row) => {
const resKey = `${row.getValue(columnId)}`
const resKey = `${row.getGroupingValue(columnId)}`
const previous = map.get(resKey)
if (!previous) {
map.set(resKey, [row])
Expand Down
Loading