Skip to content

Commit d7f99a1

Browse files
authored
feat: new getGroupingValue column option for avoiding false duplicate grouping (#4833)
* feat: add getGroupingValue columnDef option * docs: add getGroupingValue docs
1 parent 367a272 commit d7f99a1

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

docs/api/features/grouping.md

+16
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ enableGrouping?: boolean
112112
113113
Enables/disables grouping for this column.
114114
115+
### `getGroupingValue`
116+
117+
```tsx
118+
getGroupingValue?: (row: TData) => any
119+
```
120+
121+
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.
122+
115123
## Column API
116124
117125
### `aggregationFn`
@@ -202,6 +210,14 @@ getIsGrouped: () => boolean
202210
203211
Returns whether or not the row is currently grouped.
204212
213+
### `getGroupingValue`
214+
215+
```tsx
216+
getGroupingValue: (columnId: string) => unknown
217+
```
218+
219+
Returns the grouping value for any row and column (including leaf rows).
220+
205221
## Table Options
206222
207223
### `aggregationFns`

examples/react/grouping/src/main.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ function App() {
2828
accessorKey: 'firstName',
2929
header: 'First Name',
3030
cell: info => info.getValue(),
31+
/**
32+
* override the value used for row grouping
33+
* (otherwise, defaults to the value derived from accessorKey / accessorFn)
34+
*/
35+
getGroupingValue: row => `${row.firstName} ${row.lastName}`,
3136
},
3237
{
3338
accessorFn: row => row.lastName,

packages/table-core/src/features/Grouping.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export interface GroupingColumnDef<TData extends RowData, TValue> {
4040
ReturnType<Cell<TData, TValue>['getContext']>
4141
>
4242
enableGrouping?: boolean
43+
getGroupingValue?: (row: TData) => any
4344
}
4445

4546
export interface GroupingColumn<TData extends RowData> {
@@ -56,6 +57,7 @@ export interface GroupingRow {
5657
groupingColumnId?: string
5758
groupingValue?: unknown
5859
getIsGrouped: () => boolean
60+
getGroupingValue: (columnId: string) => unknown
5961
_groupingValuesCache: Record<string, any>
6062
}
6163

@@ -229,9 +231,29 @@ export const Grouping: TableFeature = {
229231
}
230232
},
231233

232-
createRow: <TData extends RowData>(row: Row<TData>): GroupingRow => {
234+
createRow: <TData extends RowData>(
235+
row: Row<TData>,
236+
table: Table<TData>
237+
): GroupingRow => {
233238
return {
234239
getIsGrouped: () => !!row.groupingColumnId,
240+
getGroupingValue: columnId => {
241+
if (row._groupingValuesCache.hasOwnProperty(columnId)) {
242+
return row._groupingValuesCache[columnId]
243+
}
244+
245+
const column = table.getColumn(columnId)
246+
247+
if (!column?.columnDef.getGroupingValue) {
248+
return row.getValue(columnId)
249+
}
250+
251+
row._groupingValuesCache[columnId] = column.columnDef.getGroupingValue(
252+
row.original
253+
)
254+
255+
return row._groupingValuesCache[columnId]
256+
},
235257
_groupingValuesCache: {},
236258
}
237259
},

packages/table-core/src/utils/getGroupedRowModel.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ function groupBy<TData extends RowData>(rows: Row<TData>[], columnId: string) {
173173
const groupMap = new Map<any, Row<TData>[]>()
174174

175175
return rows.reduce((map, row) => {
176-
const resKey = `${row.getValue(columnId)}`
176+
const resKey = `${row.getGroupingValue(columnId)}`
177177
const previous = map.get(resKey)
178178
if (!previous) {
179179
map.set(resKey, [row])

0 commit comments

Comments
 (0)