Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,16 @@ export interface ColumnDef_ColumnGrouping<
*/
enableGrouping?: boolean
/**
* 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.
*/
getGroupingValue?: (row: TData) => any
* Returns the value used to group rows for this column.
*
* When omitted, grouping uses the value derived from this column's
* `accessorKey` or `accessorFn`.
*/
getGroupingValue?: (
originalRow: TData,
index: number,
row: Row<TFeatures, TData>,
) => any
}

export interface Column_ColumnGrouping<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ export function row_getGroupingValue<
if (row._groupingValuesCache) {
row._groupingValuesCache[columnId] = column.columnDef.getGroupingValue(
row.original,
row.index,
row,
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { describe, expect, it } from 'vitest'
import {
aggregationFns,
columnGroupingFeature,
constructTable,
coreFeatures,
createGroupedRowModel,
} from '../../../../src'
import { storeReactivityBindings } from '../../../../src/store-reactivity-bindings'
import type { ColumnDef } from '../../../../src'

const features = {
...coreFeatures,
columnGroupingFeature,
coreReactivityFeature: storeReactivityBindings(),
groupedRowModel: createGroupedRowModel(),
aggregationFns,
}

interface TestRow {
bucket: string
label: string
}

describe('columnGroupingFeature', () => {
it('passes the original row, row index, and row instance to getGroupingValue', () => {
const data: Array<TestRow> = [
{ bucket: 'alpha', label: 'first' },
{ bucket: 'alpha', label: 'second' },
]
const calls: Array<{
originalRow: TestRow
index: number
rowId: string
rowIndex: number
rowOriginal: TestRow
}> = []
const columns: Array<ColumnDef<typeof features, TestRow, any>> = [
{
id: 'bucket',
accessorKey: 'bucket',
getGroupingValue: (originalRow, index, row) => {
calls.push({
originalRow,
index,
rowId: row.id,
rowIndex: row.index,
rowOriginal: row.original,
})

return `${originalRow.bucket}-${index}`
},
},
{
id: 'label',
accessorKey: 'label',
},
]

const table = constructTable<typeof features, TestRow>({
features,
renderFallbackValue: '',
data,
columns,
initialState: {
grouping: ['bucket'],
},
})

expect(
table.getGroupedRowModel().rows.map((row) => row.groupingValue),
).toEqual(['alpha-0', 'alpha-1'])
expect(calls).toEqual([
{
originalRow: data[0],
index: 0,
rowId: '0',
rowIndex: 0,
rowOriginal: data[0],
},
{
originalRow: data[1],
index: 1,
rowId: '1',
rowIndex: 1,
rowOriginal: data[1],
},
])
})
})
Loading