Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ag-Grid row not deleting

I am trying to delete a row from my ag-Grid like so:

let alertRow : RowNode = this.gridApi.getRowNode(rowIndex);
console.log(alertRow);
this.gridApi.updateRowData({remove: [alertRow]});

It gives me this error:

ag-Grid: could not find data item as object was not found

I can see in console the RowNode is the correct node and exists. I am also able to use updateRowData() to add new rows in fine but trying to remove gives this error.

I am using ag-Grid in Angular 6 component.

Why is removing not working?

like image 788
Guerrilla Avatar asked Oct 04 '18 20:10

Guerrilla


People also ask

How do you delete a row on Ag grid?

Sometimes your users want to remove rows and cells in AG Grid by pressing Backspace or Delete keys. The following example uses api. applyTransaction ({...}) to remove selected rows, when the user presses BACKSPACE or DELETE keys.

How do you update AG grid row data?

The easiest way to update data inside the grid is to replace the data you gave it with a fresh set of data. This is done by either updating the rowData bound property (if using a framework) or calling api. setRowData(newData) .

How do you get rowData on Ag grid?

The easiest way to get a Row Node is by its Row ID. The ID is either provided by you using the grid callback getRowId() , or generated by the grid using an internal sequence.


2 Answers

Replace updateRowData({remove:[alerRow]}) by

updateRowData({remove:[alertRow.data]}))

updated doc

... If you are not using ID's, then the grid will match the rows based on object reference.

like image 91
un.spike Avatar answered Sep 23 '22 00:09

un.spike


Not entirely sure this is the right place/way to add on to un.spike's answer, but if you're using the getSelectedNodes method to obtain the data you're trying to delete, the syntax is a little different (requires indexing into the selected node).

(Both assume you're using single row selection)

getSelectedNodes

const selectedNode = this.gridApi.getSelectedNodes();
this.gridApi.updateRowData({ remove: [selectedNode[0].data] });

And, for "completeness" sake,
getSelectedRows (even though the API suggests using getSelectedNodes)

const selectedRow = this.gridApi.getSelectedRows();
this.gridApi.updateRowData({ remove: selectedRow });
like image 29
Gregg L Avatar answered Sep 19 '22 00:09

Gregg L