Skip to content

Commit

Permalink
feat: new getGroupingValue column option for avoiding false duplicate…
Browse files Browse the repository at this point in the history
… grouping (#4833)

* feat: add getGroupingValue columnDef option

* docs: add getGroupingValue docs
  • Loading branch information
tombuntus committed May 2, 2023
1 parent 367a272 commit d7f99a1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
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

0 comments on commit d7f99a1

Please sign in to comment.