Expressions can be used in two different ways as follows:
Column Definition Expressions: Inside column definitions instead of functions for valueGetter
, valueSetter
, valueFormatter
and valueParser
.
Cell Expressions: Inside cells within the grid, similar to placing expressions in cells in Excel.
Expressions can be used inside column definitions instead of using functions for the getters, setters, formatters and parsers. To use an expression instead of a function, just put the body of the function into a string.
const gridOptions = {
columnDefs: [
{
field: 'employee',
valueGetter: params => params.data.firstName,
valueFormatter: params => params.value.toUpperCase(),
},
{
field: 'manager',
valueGetter: 'data.firstName',
valueFormatter: 'value.toUpperCase()'
}
],
}
Example Column Definition Expressions Copy Link
In this example string expressions are used instead of functions for valueGetter
, valueSetter
, valueFormatter
and valueParser
.
The following variables are available to the expression with the following params mapping:
x
=> params.valuevalue
=> params.valueoldValue
=> params.oldValuenewValue
=> params.newValuenode
=> params.nodedata
=> params.datacolDef
=> params.colDefcolumn
=> params.columncolumnGroup
=> params.columnGroupgetValue
=> params.getValueapi
=> params.apictx
=> params.contextFor example, for valueFormatter
's, you can access to the value via the 'x' and 'value' attributes. However in valueGetter
's, the 'x' and 'value' will be undefined as these are not part of the valueGetter
params.
Expressions and functions are two ways of achieving identical results. So why have two methods?
The advantage of functions is that they are easier to work with for you. Functions will be treated by your IDE as functions and thus benefit from compile time checks, debugging etc.
The advantage of expressions is that they are more compact, and it keeps your column definitions as simple JSON objects (just strings, no functions) which makes them candidates for saving in offline storage (e.g. storing a report definition in a database).
String based expressions are parsed and evaluated as JavaScript. Use of this form of expression may require configuration of your Content Security Policy.
Functions are recommended for most use cases, unless there is a specific need for string-based expressions.
Cell Expressions Copy LinkAbove we saw how you can have expressions
instead of valueGetters
. A shortcoming of this approach is that the expression belongs to the column and cannot be defined as part of the data, or in other words, the expression is for the entire column, it cannot be set to a particular cell.
Cell Expressions bring the expression power to the cell level, so your grid can act similar to how spreadsheets work.
Although you can put expressions into cells like Excel, the intention is that your application will decide what the expressions are. It is not intended that you give this power to your user and have the cells editable. This is because AG Grid is not trying to give Excel expressions to the user, rather AG Grid is giving you, the developer, the power to design reports and include JavaScript logic inside the cells.
To enable cell expressions, set enableCellExpressions=true
in the gridOptions. Then, whenever the grid comes across a value starting with '=', it will treat it as an expression.
The cell expressions have the same parameters of value getter expressions.
Because you have access to the context (ctx) in your expression, you can add functions to the context to be available in your expressions. This allows you limitless power in what you can calculate for your expression. For example, you could provide a function that takes values from outside of the grid.
Example Cell Expressions Copy LinkThis example demonstrates cell expressions. The second column values in the LHS (Left-Hand Side) grid all have expressions. The following can be noted:
When you provide an expression to the grid, the grid converts the expression into a function for you and then executes the function. Consider the example below, the example provides data.firstName
as the expression. This snippet of code then gets wrapped into a function with all the params attributes as function attributes.
colDef.valueGetter = 'data.firstName';
___compiledValueGetter = (node, data, colDef, column, api, context, getValue) => {
return data.firstName;
}
If your expression has the word return
in it, then the grid will assume it is a multi line expression and will not wrap it.
If your value getter does not have the word return
in it, then the grid will insert the return
statement and the ;
for you.
If your expression has many lines, then you will need to provide the ;
at the end of each line and also provide the return
statement.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4