getGroupingValue takes the original row only #6279
-
|
Currently, it seems that the code for getting the grouping value does this which means it passes the original row only. Our problem is that we rely on accessor functions as data mapping mechanism, and this architecture prevents us from defining a custom groupingValue and an accessor function at the same time, since you need the row index (of the row, not the original) to call the accessor function. We currently have a workaround involving a cache to make the values returned by the accessor function stable, but this seams suboptimal. Would it be a useful addition for the table to pass the row instead of row.original or does this has some side effects i am not aware of? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
|
Yeah, this is a real limitation. The cleanest workaround without patching the library: compute the mapped values upfront and store them on the original data. Instead of relying on the accessor to do the mapping at render time, pre-process your data array before passing it to const processedData = useMemo(() =>
rawData.map((item, index) => ({
...item,
_mappedValue: computeValue(item, index), // whatever your accessor does
})),
[rawData]
);Then your If that doesn't fit your architecture (e.g. the mapping depends on other table state), then passing the full |
Beta Was this translation helpful? Give feedback.
-
|
It would also already help if getGroupingValue would receive the index to the original row, so one can call the same accessor with these params (Because at this point the limitation might exist because the row models do not exist yet) |
Beta Was this translation helpful? Give feedback.
-
|
This is already addressed in the upcoming v9, currently in beta, worth checking before building a workaround on top of v8. Comparing the source directly: on the current stable v8.21.3, For v8 in the meantime, since const indexByRef = new WeakMap(data.map((row, i) => [row, i]))
// in the columnDef:
getGroupingValue: (originalRow) => {
const index = indexByRef.get(originalRow)
return computeGroupKey(originalRow, index)
}That only holds if |
Beta Was this translation helpful? Give feedback.
This is already addressed in the upcoming v9, currently in beta, worth checking before building a workaround on top of v8.
Comparing the source directly: on the current stable v8.21.3,
ColumnGrouping.tstypesgetGroupingValueas(row: TData) => any, a single argument, exactly the limitation described here. On the v9 beta branch,columnGroupingFeature.types.tshas already changed the signature to(originalRow: TData, index: number, row: Row<TFeatures, TData>) => any, androw_getGroupingValueincolumnGroupingFeature.utils.tscalls it ascolumn.columnDef.getGroupingValue(row.original, row.index, row), so both the index and the full row (with all its own methods) are already there.For v8 in…