UNPKG

@tanstack/table-core

Version:

Headless UI for building powerful tables & datagrids for TS/JS.

71 lines (69 loc) 1.95 kB
import { hasOwn } from "../utils.js"; import { constructRow } from "../core/rows/constructRow.js"; //#region src/worker/rebuildRowModel.ts function collectLeafRows(subRows, out) { for (let i = 0; i < subRows.length; i++) { const row = subRows[i]; if (row.groupingColumnId == null) out.push(row); else collectLeafRows(row.subRows, out); } } function rebuildRowModel(table, payload, resetDepths) { const core = table.getCoreRowModel(); if (payload.kind === "flat") { const { indices } = payload; const rows = new Array(indices.length); for (let i = 0; i < indices.length; i++) { const row = core.flatRows[indices[i]]; if (resetDepths) { row.depth = 0; row.parentId = void 0; } rows[i] = row; } return { rows, flatRows: rows, rowsById: core.rowsById }; } const flatRows = []; const rowsById = Object.create(core.rowsById); const rebuildRows = (nodes, depth, parentId) => { const rows = new Array(nodes.length); for (let i = 0; i < nodes.length; i++) { const node = nodes[i]; if (typeof node === "number") { const row = core.flatRows[node]; row.depth = depth; row.parentId = parentId; flatRows.push(row); rows[i] = row; continue; } const subRows = rebuildRows(node.children, depth + 1, node.id); const leafRows = []; collectLeafRows(subRows, leafRows); const row = constructRow(table, node.id, leafRows[0]?.original, node.index, depth, void 0, parentId); const aggregates = node.aggregates; Object.assign(row, { groupingColumnId: node.groupingColumnId, groupingValue: node.groupingValue, subRows, leafRows, getValue: (columnId) => hasOwn(aggregates, columnId) ? aggregates[columnId] : void 0 }); flatRows.push(row); rowsById[node.id] = row; rows[i] = row; } return rows; }; return { rows: rebuildRows(payload.children, 0, void 0), flatRows, rowsById }; } //#endregion export { rebuildRowModel };