I have an ag grid where i am trying to delete a row...I am able to remove the row from data source using "splice" technique,after that i want to refresh the table.But it is showing error.This is the code which i am using to delete a row
selectedvalue={} //this holds the selected row value
rowData=[]; //this holds all the row data
onRowSelected(event) {
this.selectedvalue = event;
}
deletebtn() {
for (let i = 0; i < this.rowData.length; i++) {
if (this.selectedvalue.node.data.make === this.rowData[i].make) {
this.rowData.splice(i, 1);
this.gridOptions.api.refreshView();
}
}
}
It is showing erroe something like this--> Cannot read property 'refreshView' of undefined...How can watch the changes made in table after row delete.
Refresh Cells: api. refreshCells(cellRefreshParams) - Gets the grid to refresh all cells. Change detection will be used to refresh only cells whose display cell values are out of sync with the actual value. If using a cellRenderer with a refresh method, the refresh method will get called.
Edit: This solution is for version 3.3.x (updated plnkr link)
You should set the rows into the grid again: after your splice:
gridOptions.api.setRowData(gridOptions.rowData)
Maybe this plunkr helps https://plnkr.co/plunk/0k4sYa
The author of ag-grid explains this in the ag-grid forum The forum no longer exist
There is a more efficient way described in the documentation : Transaction Update API.
You must use the method api.applyTransaction
after the rows are deleted :
gridOptions.api.applyTransaction({ remove: [array of rows you have deleted]});
For example with just one row deleted :
gridOptions.api.applyTransaction({ remove: [this.rowData[i]]})
With this method, the grid will just update the rows in parameters and keeps all other view states (order, ...).
To complete the answer, there are also parameters to update the grid when adding or modifying one or more lines.
When adding :
gridOptions.api.applyTransaction({ add: [array of rows you have added]});
When modifying :
gridOptions.api.applyTransaction({ update: [array of rows you have changed]});
Remarks : for older versions of AG Grid the method was api.updateRowData instead of api.applyTransaction
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