I am using ag-Grid with TypeScript and trying to create an async value setter in my column definition.
const columnDefs: ColDef[] = [
{
headerName: 'Name',
field: 'name',
editable: true,
valueSetter: async (params: ValueSetterParams): Promise<boolean> => {
return await doSomething(params); // Will return the change boolean
}
},
//...other columns
But TypeScript complains that this signature is not supported because the colDef.d.ts
definition file has
valueSetter?: ((params: ValueSetterParams) => boolean) | string;
However, if I just use // @ts-ignore
to ignore the TypeScript error it seems to be respecting the await
and my code executes in the right order.
Is it just an issue with the TypeScript definition file? Or does ag-Grid not support async value setters?
Some environment info:
Value Setter const gridOptions = { columnDefs: [ // Option 1: using field for getting and setting the value { field: 'name' }, // Options 2: using valueGetter and valueSetter - value getter used to get data { valueGetter: params => { return params.data.name; }, valueSetter: params => { params.data.name = params.
field , you can use colDef. valueGetter to provide a function for getting cell values. This is more flexible than providing field values for specific cells. Value Formatters: Use formatters to format values.
when you pass the column definition to the ag-grid instance, it will have your function bound to cellrenderer attribute for that column. So when in its build lifecycle it checks the value at cellRenderer it will see your function and fire it then.
Saving / Restoring ChartsCreate a range chart from the grid, which will be shown in a container below the grid. Change the chart type, theme, data and/or formatting in order to see the changes restored later. Click "Save chart" to persist a model of the visible chart into a local variable.
Ag-grid doesn't provide support for this, but you can still accomplish it. Instead of returning the promise, you still have to return a boolean. Then you have to manually refresh the cells after the promise returns:
valueSetter: (params: ValueSetterParams) => {
doSomething(params).then(function() {
params.api.refreshCells({
rowNodes: [params.node],
columns: [params.column]
});
});
return false;
}
Here is an example, where I just use a setTimeout
to emulate an async operation: https://stackblitz.com/edit/ag-grid-typescript-async-value-setter?file=src/main.ts
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With