@1771technologies/lytenyte-pro
Version:
Blazingly fast headless React data grid with 100s of features.
66 lines (65 loc) • 2.08 kB
JavaScript
import { COLUMN_MARKER_ID, computePathMatrix, GROUP_COLUMN_PREFIX, } from "@1771technologies/lytenyte-shared";
export function getDataRect({ rows, columnStart, columnEnd, uniformGroupHeaders, visible, columnField, }) {
const data = [];
const columns = [];
for (let i = columnStart; i < columnEnd && i < visible.length; i++) {
const column = visible[i];
if (column.id === COLUMN_MARKER_ID)
continue;
columns.push(column);
}
const groupHeaders = [];
const hierarchy = computePathMatrix(visible);
for (let levelIndex = 0; levelIndex < hierarchy.length; levelIndex++) {
const level = hierarchy[levelIndex];
const seen = new Set();
const header = [];
for (let i = 0; i < columns.length; i++) {
const value = level[i];
if (!value) {
header.push(null);
continue;
}
if (seen.has(value.idOccurrence)) {
header.push("");
continue;
}
if (!uniformGroupHeaders)
seen.add(value.idOccurrence);
const group = value.groupPath.at(-1);
header.push(group);
}
groupHeaders.push(header);
}
let headers = [];
headers = [];
for (const v of columns) {
headers.push(v.name ?? v.id);
}
for (const row of Object.values(rows)) {
const dataForRow = [];
for (const column of columns) {
let field;
if (!row) {
field = null;
}
else if (column.id.startsWith(GROUP_COLUMN_PREFIX)) {
if (row.kind === "branch")
field = row.key;
else
field = "";
}
else {
field = columnField(column, { kind: row.kind, data: row.data });
}
dataForRow.push(field);
}
data.push(dataForRow);
}
return {
data,
groupHeaders,
columns,
headers,
};
}