Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use an async value setter in ag-Grid (TypeScript errors)?

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:

  • ag-Grid 22.1.0
  • TypeScript 3.7.2
  • Aurelia (which uses WebPack, npm, etc.)
  • WebStorm 2019.3.1
like image 929
lebolo Avatar asked Jan 09 '20 13:01

lebolo


People also ask

How do you set Ag grid value?

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.

How do I get cell value on Ag grid?

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.

How do you call a function on Ag grid?

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.

How do you save Ag grid data?

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.


1 Answers

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

like image 86
Matt Nienow Avatar answered Oct 05 '22 00:10

Matt Nienow