Rajesh Kumar Duraisamy
Rajesh Kumar Duraisamy

Reputation: 807

ag grid row data is updating after reset the row data

I am trying to update my row data in ag grid after an edit. But the data is not updated in the grid I tried with

this.gridApi.setRowData(this.rowData);
this.rowData = this.countryData;
this.gridApi.refreshCells({ force: true });

But none of the options are working here.

Here is my stackblitz url

https://stackblitz.com/edit/ag-grid-angular-hello-world-uh7vqn?file=src%2Fapp%2Fapp.component.ts

Please help me that what is way to reset the data in ag gird After clicking the reset button i like to refresh the agrid with old value i.e the edited value shoudld be replaced by original value

The red marked value should be replaced by old value

enter image description here

Upvotes: 0

Views: 4904

Answers (2)

Deepak Jha
Deepak Jha

Reputation: 1609

https://stackblitz.com/edit/ag-grid-angular-hello-world-yr6yth?file=src%2Fapp%2Fapp.component.ts

I think I got your problem, this is due to the object deep copy. When you receive data you are essentially copying the same object twice and when reference of both the object remain same as a result you change one object thinking it will not change other but it changes other as well. You need to create different object for backup data with same value and this should work

    this.getCountryList().subscribe(data => {
      this.rowData = JSON.parse(JSON.stringify(data));
      this.countryData = JSON.parse(JSON.stringify(data));
    });

Upvotes: 0

Zunayed Shahriar
Zunayed Shahriar

Reputation: 2743

rowData and countryData are assigned with the same dataset.

So, when you are changing grid value countryData is also being modified.

So, you need to deep copy your raw data when you are populating countryData for the first time. And when trying to reset, populate rowData from deep copied data from countryData. So that, you can there are no reference between rowData and countryData.

A way to deep copy is to stringify and parse the same data. Like:

this.countryData = JSON.parse(JSON.stringify(data));

Working demo: https://stackblitz.com/edit/ag-grid-angular-hello-world-qr5cfk?file=src%2Fapp%2Fapp.component.ts

Upvotes: 1

Related Questions