0

I just wanted to give the functionality of edit in my grid and my data was coming from a NGRX state. So, I was not able to edit directly because it was throwing an error saying that can't assign value to read-only object. So, after searching on stackoverflow, I found this AG-Grid: Cannot update field in Grid as it is read only.

So, I'm just trying to use this valueSetter as suggested in the post in the following way:-

this.columnDefs = Object.assign([], this.columnDefs);
console.log('coldDef init ', this.columnDefs);
this.columnDefs = this.columnDefs.map(obj=>({
  ...obj,
  valueSetter: (params:any) => {
    console.log("##newValue##",params.newValue);
    const paramsCopy = {...params.data}
    paramsCopy[obj.field]=params.newValue;
    console.log(params);
    params.data=paramsCopy;
    console.log(params);
    return true;
  }
}));

In the logs I can see the new value.

Please click here to see logs

But, I can't see my new value on the grid. Am I missing something?

Please let me know if you need more details.

Thank you!!

  • just writing `params.data[obj.field] = params.newValue` would not work? – Dorin Baba Sep 05 '21 at 07:35
  • But I can see the updated value in the console. – Hunny Chawla Sep 05 '21 at 07:54
  • I see you are creating a shallow copy of your data and maybe this confuses AG Grid and it does not know that cell to update because the whole data has been updated. Try setting the property of the data object without creating another data object – Dorin Baba Sep 05 '21 at 08:07
  • AG Grid has a kind of change-detection mechanism which updates cell's value when you return true from the `valueSetter` method – Dorin Baba Sep 05 '21 at 08:09
  • if I'm trying this: params.data[obj.field]=params.newValue; getting following error: ERROR TypeError: Cannot assign to read only property 'title' of object '[object Object]' at valueSetter (generic-grid.component.ts:103) – Hunny Chawla Sep 05 '21 at 08:19
  • There is also an `api.refreshCells()` that might help you, but I'm not sure... – Dorin Baba Sep 05 '21 at 08:49
  • tried that too, but no gain..... – Hunny Chawla Sep 05 '21 at 09:07

0 Answers0