I want to find out which row has edited In Ag-Grid? I want to save only those rows which are edited? so is there any method so that i can find out which row has edited?
-
... not clear how you trying to update your data, pushing all to the server on each update? what method from `grid-API` your are planning to use? – un.spike Sep 17 '18 at 11:25
-
Ex - At the load time 5 rows came and i added only one row then i click on save button. So is there any method so that i can get only that row which i edited. @un.spike – Vishal Mittal Sep 17 '18 at 12:10
-
global `save` button ? not related with `node` (row) ? – un.spike Sep 17 '18 at 12:28
-
yes. correct. @un.spike – Vishal Mittal Sep 17 '18 at 12:48
2 Answers
You can use onCellEditingStopped()
event for ag-grid.
html
<ag-grid-angular
...
(cellEditingStopped)="onCellEditingStopped($event)"
...
></ag-grid-angular>
ts
onCellEditingStopped(e) {
console.log(e.rowIndex);
}

- 4,777
- 5
- 23
- 41
-
It means i have to add a variable in my model may be named As 'Edited' : true by default it'll be false and on this function onCellEditingStopped i have to set it true then i can easily find all the dited rows by setting a loop on my datamodel. – Vishal Mittal Sep 18 '18 at 06:45
global save button ? not related with a node (row)
yes. correct
Ok, on this case you need to track as an example via Id
what data is new. So, let's say on your data you have unique
identifier - which will represent Primary
key or something like that - and this key should be handled on back-end or database side.
Now, to get only 'new' nodes, you can filter grid-data
like that:
let newData = [];
this.gridApi.forEachNode(node=>{
if(!node.data.id)
newData.push(node.data)
})
But I'm recommending to avoid multiple insert
and handle each creation separately like that :
handleInsert():void{
let yourDataModel = {
...
}
this.sampleApiService.sampleRequest(dataModel).subscribe(result => {
this.gridApi.updateRowData({add:[result], addIndex:0});
})
}
result
- should return modified dataModel
with already defined Id
In this case, you will be able to update grid-data
and be sure that data is correct and already inserted into your storage
.
Update added example for update
Instead of (cellEditingStopped)
event you can also use valueSetter
for tracking cell updates
handleUpdateValue(params: ValueSetterParams){
// see if values are different if you have a complex object,
// it would be more complicated to do this.
if (params.oldValue!==params.newValue) {
// params.colDef.field - updated cell ID
... handle update logic here - call API and so on ...
// get grid to refresh the cell
return true;
} else {
// no change, so no refresh needed
return false;
}
}

- 4,857
- 2
- 18
- 38
-
It can be useful only in newly added rows not if i am updating any of row because existing rows Id will be positive. – Vishal Mittal Sep 18 '18 at 06:46