@libregrid/calculated-columns
v1.3.5
Published
Calculated columns (read-only derived data) for AG Grid Community — part of LibreGrid
Maintainers
Readme
@libregrid/calculated-columns
Add read-only columns derived from other values in the same row, such as
[revenue] - [cost]. Define expressions in code or let users create and edit
calculated columns through the column menu.
Install
npm install "ag-grid-community@^36.2.0" @libregrid/calculated-columnsRequires ag-grid-community >=36.1.0 <37 as a peer dependency.
Usage
In a browser TypeScript project, add a grid container before running the code:
<div id="grid" style="height: 400px"></div>import { ModuleRegistry, AllCommunityModule, createGrid } from 'ag-grid-community';
import { CalculatedColumnsModule } from '@libregrid/calculated-columns';
ModuleRegistry.registerModules([AllCommunityModule, CalculatedColumnsModule]);
const api = createGrid(document.querySelector<HTMLElement>('#grid')!, {
calculatedColumns: true, // or { dataTypes, expressionPickers, applyMode, suppressColumnHighlighting }
columnDefs: [
{ field: 'revenue' },
{ field: 'cost' },
{ colId: 'profit', calculatedExpression: '[revenue] - [cost]', cellDataType: 'number' },
],
rowData: [{ revenue: 120, cost: 40 }],
});Register ColumnMenuModule from @libregrid/menu to
expose the editing UI. End users add columns from the column menu (Add Calculated Column on any
column's header menu), edit them via Calculated Column → Edit Calculated
Column, and remove them via the menu or the cell context menu. Calculated
columns are read-only. Their evaluated values can be sorted, filtered, grouped,
and aggregated when the corresponding modules are registered.
Expressions
- Bracket references read same-row values by
colId(defaults tofield):[revenue] * 2. - Operators:
+ - * / ^ & = <> > < >= <= %. - Provided functions:
SUM,PRODUCT,MIN,MAX,AVERAGE,MEDIAN,POWER,RAND,NOW,TODAY,CONCAT,IF,COUNT,COUNTA,COUNTBLANK,AND,OR,NOT.SUMIF/COUNTIFaccept array arguments (cell ranges arrive with the Formulas feature). - Values pills insert a matching inline editor in the expression canvas for
text, numbers, booleans, and ISO dates. The date control serializes to a
quoted
"YYYY-MM-DD"literal when it loses focus. - Errors render as spreadsheet codes in the cell (
#REF!,#NAME?,#CIRCREF!,#PARSE!,#VALUE!,#DIV/0!,#ERROR!) with the Community formula-error styling and tooltip.
Events
calculatedColumnCreated/calculatedColumnRemovedcalculatedColumnExpressionChanged(oldExpressionincluded)calculatedColumnValidationStateChanged(valid,reasonon flips)
Notes
- Dialog-created columns persist through Grid State (
api.getState()/initialState) via the Community user-column layer. calculatedColumns: false(or unset) leaves declared calculated columns blank and hides the menu entries.
Angular
Use the same column definitions and grid options with ag-grid-angular.
Register the modules shown above through provideLibreGrid from
@libregrid/angular. See the
Angular setup for a complete component and bootstrap example.
License
MIT — see LICENSE and NOTICE. LibreGrid is an independent project and is not affiliated with, endorsed by, or sponsored by AG Grid Ltd.
