How to remove all rows in AgGrid

Mi Guoliang

There is no data API in AgGrid, therefore we use Grid API instead.

// .ts file, and variable *grid* is declared like:
// @ViewChild('grid', { static: true }) grid: AgGridAngular;
this.grid.gridOptions.api.setColumnDefs([]);
const oldData = [];
this.grid.gridOptions.api.forEachNode(r => oldData.push(r.data));
this.grid.gridOptions.api.applyTransaction({ remove: oldData });

Conclusion

If we want to clear grid properly, we need do step by step:

  1. Reset columnDefs to an empty array.
  2. Fetch all existing raw data.
  3. Remove all existing raw data by applyTransaction.

If you like my share, you can:

• Follow My Programming Experiences Page on Facebook.

• Follow My Twitter.

• Subscribe to my mail list.