0

ag-grid allows string expressions in column valueGetters. For example:

{
  field: 'r',
  headerName: 'Radius'
},
{
  field: 'circumference',
  valueGetter: 'Math.PI * data.r ** 2',
}

I am wondering if the expression parser can be switched to mathjs because its syntax is much nicer. For example, the same expression above can be written as pi r ^ 2. How can I do this?

I have a workaround to achieved this by using a ValueGetter function, but that defeats the purpose of defining the expression in the column definition. See below:

import { evaluate } from 'mathjs';


const evaluateExpression = (params: ValueGetterParams) => {
  return evaluate('pi r ^ 2', params.data);
};

// Column definitions
{
  field: 'r',
  headerName: 'Radius'
},
{
  field: 'circumference',
  valueGetter: evaluateExpression,
}

Edit

To clarify, the workaround uses a valueGetter function, which means that the column definitions cannot be stored as a JSON object, which is what I am after. From the AG Grid docs:

The advantage of expressions are that they keep your column definitions as simple JSON objects (just strings, no functions) which makes them candidates for saving in offline storage (eg storing a report definition in a database).

Naresh
  • 23,937
  • 33
  • 132
  • 204

1 Answers1

0

Do you need it as a string expression?

Why don't you just define it like the code below if you want to define the expression in the column definition?

// Column definition

{
  field: 'circumference',
  valueGetter: (p) => evaluate('pi r ^ 2', p.data),
}
  • Well, that works - it's essentially the second solution listed in my question - just that it specifies the function external to the column definition. The real issue is that as soon as the valueGetter becomes a function, the column definitions cannot be saved in persistent storage. The advantage of pure expressions is that I can save/retrieve them from persistent storage. From ag-grid docs: "[expressions] keep your column definitions as simple JSON objects (just strings, no functions) which makes them candidates for saving in offline storage (eg storing a report definition in a database)." – Naresh Oct 03 '22 at 19:50