-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
getGroupedRowModel.ts
185 lines (157 loc) 路 5.93 KB
/
getGroupedRowModel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { createRow } from '../core/row'
import { Table, Row, RowModel, RowData } from '../types'
import { flattenBy, memo } from '../utils'
export function getGroupedRowModel<TData extends RowData>(): (
table: Table<TData>
) => () => RowModel<TData> {
return table =>
memo(
() => [table.getState().grouping, table.getPreGroupedRowModel()],
(grouping, rowModel) => {
if (!rowModel.rows.length || !grouping.length) {
return rowModel
}
// Filter the grouping list down to columns that exist
const existingGrouping = grouping.filter(columnId =>
table.getColumn(columnId)
)
const groupedFlatRows: Row<TData>[] = []
const groupedRowsById: Record<string, Row<TData>> = {}
// const onlyGroupedFlatRows: Row[] = [];
// const onlyGroupedRowsById: Record<RowId, Row> = {};
// const nonGroupedFlatRows: Row[] = [];
// const nonGroupedRowsById: Record<RowId, Row> = {};
// Recursively group the data
const groupUpRecursively = (
rows: Row<TData>[],
depth = 0,
parentId?: string
) => {
// Grouping depth has been been met
// Stop grouping and simply rewrite thd depth and row relationships
if (depth >= existingGrouping.length) {
return rows.map(row => {
row.depth = depth
groupedFlatRows.push(row)
groupedRowsById[row.id] = row
if (row.subRows) {
row.subRows = groupUpRecursively(row.subRows, depth + 1, row.id)
}
return row
})
}
const columnId: string = existingGrouping[depth]!
// Group the rows together for this level
const rowGroupsMap = groupBy(rows, columnId)
// Peform aggregations for each group
const aggregatedGroupedRows = Array.from(rowGroupsMap.entries()).map(
([groupingValue, groupedRows], index) => {
let id = `${columnId}:${groupingValue}`
id = parentId ? `${parentId}>${id}` : id
// First, Recurse to group sub rows before aggregation
const subRows = groupUpRecursively(groupedRows, depth + 1, id)
// Flatten the leaf rows of the rows in this group
const leafRows = depth
? flattenBy(groupedRows, row => row.subRows)
: groupedRows
const row = createRow(
table,
id,
leafRows[0]!.original,
index,
depth,
undefined,
parentId
)
Object.assign(row, {
groupingColumnId: columnId,
groupingValue,
subRows,
leafRows,
getValue: (columnId: string) => {
// Don't aggregate columns that are in the grouping
if (existingGrouping.includes(columnId)) {
if (row._valuesCache.hasOwnProperty(columnId)) {
return row._valuesCache[columnId]
}
if (groupedRows[0]) {
row._valuesCache[columnId] =
groupedRows[0].getValue(columnId) ?? undefined
}
return row._valuesCache[columnId]
}
if (row._groupingValuesCache.hasOwnProperty(columnId)) {
return row._groupingValuesCache[columnId]
}
// Aggregate the values
const column = table.getColumn(columnId)
const aggregateFn = column?.getAggregationFn()
if (aggregateFn) {
row._groupingValuesCache[columnId] = aggregateFn(
columnId,
leafRows,
groupedRows
)
return row._groupingValuesCache[columnId]
}
},
})
subRows.forEach(subRow => {
groupedFlatRows.push(subRow)
groupedRowsById[subRow.id] = subRow
// if (subRow.getIsGrouped?.()) {
// onlyGroupedFlatRows.push(subRow);
// onlyGroupedRowsById[subRow.id] = subRow;
// } else {
// nonGroupedFlatRows.push(subRow);
// nonGroupedRowsById[subRow.id] = subRow;
// }
})
return row
}
)
return aggregatedGroupedRows
}
const groupedRows = groupUpRecursively(rowModel.rows, 0)
groupedRows.forEach(subRow => {
groupedFlatRows.push(subRow)
groupedRowsById[subRow.id] = subRow
// if (subRow.getIsGrouped?.()) {
// onlyGroupedFlatRows.push(subRow);
// onlyGroupedRowsById[subRow.id] = subRow;
// } else {
// nonGroupedFlatRows.push(subRow);
// nonGroupedRowsById[subRow.id] = subRow;
// }
})
return {
rows: groupedRows,
flatRows: groupedFlatRows,
rowsById: groupedRowsById,
}
},
{
key: process.env.NODE_ENV === 'development' && 'getGroupedRowModel',
debug: () => table.options.debugAll ?? table.options.debugTable,
onChange: () => {
table._queue(() => {
table._autoResetExpanded()
table._autoResetPageIndex()
})
},
}
)
}
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.getGroupingValue(columnId)}`
const previous = map.get(resKey)
if (!previous) {
map.set(resKey, [row])
} else {
previous.push(row)
}
return map
}, groupMap)
}