21

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.

Raja Reddy
  • 417
  • 2
  • 5
  • 11

7 Answers7

36

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

Moises Sacal
  • 576
  • 5
  • 8
  • Ugh can you provide a link to the ag-grid forum where this is explained? I've registered but I'm not an Enterprise user, so maybe that's why I cannot search properly for that explanation. – Gwyneth Llewelyn Jun 11 '17 at 10:57
  • 2
    I'm not an enterprise user as well. But, you can still search the forum without one. Like this one https://ag-grid.com/forum/showthread.php?tid=3726&pid=8876&highlight=setRowData#pid8876 – Moises Sacal Jun 12 '17 at 02:40
  • 1
    Thanks! I was unable to find that in the forums, but it turns out that the answer is not entirely up-to-date. See the answer from @user3294566 below. However, your answer is correct for the early 2016 versions of agGrid; newer versions just have much better API calls to easily accomplish the removal of a row and subsequent refresh. – Gwyneth Llewelyn Jun 25 '17 at 01:46
  • The Plunker is 404 :-( Is `gridOptions.api.setRowData(gridOptions.rowData)` enough, or is it necessary to `gridOptions.api.refreshClientSideRowModel()` afterwards? – Mawg says reinstate Monica Apr 02 '20 at 20:08
  • 1
    Hi @MawgsaysreinstateMonica . I've updated the plnkr link, plnkr changed the link method. See the script for the setRowData, as for that ag-grid version, it is the only thing required. Be ware of the ag-grid version as Gwyneth mentions. – Moises Sacal Apr 05 '20 at 22:22
  • There is no forum anymore. please add updated link. thanks – MindRoasterMir Nov 20 '20 at 11:44
13

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

A.Baudouin
  • 2,855
  • 3
  • 24
  • 28
7

For better performance, use grid api calls to add/remove rows.
To insert a row at the beginning, that is copy of a selected row:

var rowData = JSON.parse(JSON.stringify(selectedNode.data));
gridOptions.api.insertItemsAtIndex(0, [rowData]);

To remove a selected row:

var selectedNodes = gridOptions.api.getSelectedNodes();
gridOptions.api.removeItems(selectedNodes);

Please insert new row only after a deep copy of original row.
Otherwise, api continues to refer to the same row.
So, the subsequent remove of new row, will remove original row from grid.

Please refer documentation for api details.
https://www.ag-grid.com/javascript-grid-insert-remove/

user3294566
  • 116
  • 1
  • 2
  • 1
    You're not using `newRow` anywhere on the insert... instead, you have `rowData`. I guess you need to change that. – Gwyneth Llewelyn Jun 11 '17 at 10:43
  • 1
    Thanks for your answer! While it's now obvious to me, there have been changes to the way agGrid works in mid-2017 — the original question was asked in 2016 — and I actually had an *outdated* version of agGrid, where your own answer (which is correct for the current version of agGrid!) did not work, i.e. the JS console got some errors for missing API functions. Indeed, you answer (which I have upvoted :-) ) is the more correct, up-to-date method to accomplish what the OP asked — there is *no* need to feed *all* the rows again to agGrid, as it seemed to be the case in early 2016. – Gwyneth Llewelyn Jun 25 '17 at 01:50
2

$events.refreshInfiniteCache();

you can use this simply for update your grid

2

Updated answer for newer versions of agGrid.

From version 23.1.0+: Use applyTransaction(transaction)

gridApi.applyTransaction({ remove: rowArray });

// similarly you can add or update using transaction
gridApi.applyTransaction({ update: rowArray});
gridApi.applyTransaction({ add: rowArray});

Where rowArray is an array of row data. Existing rows data can be accessed from RowNode.data.

Example

onRemoveFirst() {
  const firstRow = this.gridApi.getRenderedNodes()[0].data;

  if (!firstRow) return;
  this.gridApi.applyTransaction({ remove: [firstRow] });
}

onRemoveSelected() {
  const selectedData = this.gridApi.getSelectedRows();
  this.gridApi.applyTransaction({ remove: selectedData });
}

onThanos() {
  const itemsToRemove = [];

  this.gridApi.forEachNodeAfterFilterAndSort((rowNode) => {
    const shouldThanos = randomBetween(1, 100) <= 50;

    if (shouldThanos) {
      itemsToRemove.push(rowNode.data);
    }
  });
  this.gridApi.applyTransaction({ remove: itemsToRemove });
}

Live Demo

Edit 35311052/how-to-upgrade-refresh-the-ag-grid-after-row-delete/

NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
  • applyTransaction is not updating the underlying data array. How to get the full data set (including the modification) Any thoughts on that? – James Poulose Jan 01 '21 at 04:00
1

To update the data in a particular cell of aggrid

        let rowNode = f.gridApi.getRowNode(f.id);  // use to find the rowNode
        rowNode.setDataValue('myPrice','1000') // update the datavalue in the price cell

f is current node of the aggrid

Rajat
  • 152
  • 1
  • 5
0

Below is how i delete Rows from my AgGrid.

  1. Select 1 or more rows using ridApi.getSelectedNodes.

  2. Map through the selected rows and take out the index of the given row.

  3. Use array method filter to filter out all the rows that are not selected

  4. Spread the filtered rows with the old rows

    const selectedRows: RowNode[] = this.gridApi.getSelectedNodes();
    const rowsToRemove = selectedRows.map(node => node.rowIndex);
    const filteredRows = allRows.filter((_, i) => !rowsToRemove.includes(i));
    
    this.rowData = [...filteredRows] 
    

hope this help to anyone in need of it!

Bojidari
  • 85
  • 1
  • 5