LyteNyte Grid uses the aggModel
on grid state to determine how to create aggregations for rows.
Aggregations are used for the calculation of the data for group rows. Normal leaf rows do not
have aggregated data - since by definition the leaf rows are used to make up the aggregated data.
"use client";
import {
LyteNyteGrid,
useLyteNytePro,
useClientDataSource,
} from "@1771technologies/lytenyte-pro";
import "@1771technologies/lytenyte-pro/grid.css";
import { ColumnProReact } from "@1771technologies/lytenyte-pro/types";
import { bankDataSmall } from "@1771technologies/sample-data/bank-data-smaller";
import { useId } from "react";
const columns: ColumnProReact[] = [
{ id: "age", type: "number" },
{ id: "job", rowGroupable: true },
{ id: "balance", type: "number" },
{ id: "education", rowGroupable: true },
{ id: "marital" },
];
export function RowAggregations() {
const ds = useClientDataSource({
data: bankDataSmall,
});
const grid = useLyteNytePro({
gridId: useId(),
rowDataSource: ds,
columns,
rowGroupModel: ["job", "education"],
aggModel: {
age: { fn: "avg" },
job: { fn: "count" },
balance: { fn: "sum" },
},
});
return (
<div>
<div style={{ height: 500, display: "flex", flexDirection: "column" }}>
<div style={{ flex: "1" }}>
<LyteNyteGrid grid={grid} />
</div>
</div>
</div>
);
}
The aggModel
is an object, where each key corresponds to a key on the final aggregation output.
The aggregation output must be an object of key value pairs. Normally the key is the id of a column
but this is not a requirement.
Each aggModel
value is an object with a fn
key. The fn
key is used to determine which aggregation
to apply for the final aggregation output. LyteNyte Grid provides some built-in aggregation functions, but you may
also define your own.
The full type of the aggModel
is showing below:
export type AggBuiltIns =
| "sum"
| "min"
| "max"
| "avg"
| "count"
| "first"
| "last"
| "group";
export type AggModel = {
[columnId: string]: {
fn: AggBuiltIns | (string & {}) | AggFn;
};
};
Aggregations are calculated by the row data source being used. The client row data source will perform aggregation calculations on the client, whilst the server row data source will perform them on the server. Not every column must have a aggregation applied - and for many columns it makes sense for no aggregation to be applied.