Search code examples
ag-grid-ng2

Duplicating the rownode on button click in ag grid


How I can clone the row in AG grid, also cloning should have the same data whichever row was cloned. Although data can be further editable.


Solution

  • First, clone the data into a new object:

    const originalRecord: RecordType;
    ...
    const newRecord: RecordType = {...originalRecord};
    

    (You will need to use a different approach to duplicating the record if you want to include methods or you need a deep copy).

    Next, add the new record to the grid:

      this.grid.api.updateRowData({add: [newRecord]});
    

    There's an example of creating new rows here: Updating with transaction.