diff --git a/packages/table-core/src/utils/getGroupedRowModel.ts b/packages/table-core/src/utils/getGroupedRowModel.ts index b92befaf95..da97631913 100644 --- a/packages/table-core/src/utils/getGroupedRowModel.ts +++ b/packages/table-core/src/utils/getGroupedRowModel.ts @@ -1,6 +1,7 @@ import { createRow } from '../core/row' -import { Table, Row, RowModel, RowData } from '../types' +import { Row, RowData, RowModel, Table } from '../types' import { flattenBy, getMemoOptions, memo } from '../utils' +import { GroupingState } from '../features/ColumnGrouping' export function getGroupedRowModel(): ( table: Table @@ -10,6 +11,10 @@ export function getGroupedRowModel(): ( () => [table.getState().grouping, table.getPreGroupedRowModel()], (grouping, rowModel) => { if (!rowModel.rows.length || !grouping.length) { + rowModel.rows.forEach(row => { + row.depth = 0 + row.parentId = undefined + }) return rowModel } @@ -53,7 +58,7 @@ export function getGroupedRowModel(): ( // Group the rows together for this level const rowGroupsMap = groupBy(rows, columnId) - // Peform aggregations for each group + // Perform aggregations for each group const aggregatedGroupedRows = Array.from(rowGroupsMap.entries()).map( ([groupingValue, groupedRows], index) => { let id = `${columnId}:${groupingValue}` @@ -62,6 +67,10 @@ export function getGroupedRowModel(): ( // First, Recurse to group sub rows before aggregation const subRows = groupUpRecursively(groupedRows, depth + 1, id) + subRows.forEach(subRow => { + subRow.parentId = id + }) + // Flatten the leaf rows of the rows in this group const leafRows = depth ? flattenBy(groupedRows, row => row.subRows)