Is it possible to use ag-grid's 'Undo/Redo' feature when modifying data through code?

117 Views Asked by At

Can the 'Undo/Redo' functionality of ag-grid work when changes are triggered by code and not by editing the grid?

After rowNode.setDataValue on a row field, the undo: gridApi.value.undoCellEditing() does not work

It is possible to change the data from code and then invoke the undo?

Could you provide an example?

1

There are 1 best solutions below

0
Vostroknutov Vitaliy On

For working ag grid undo redo when setDataValue called I decided with the following code:

// for undo redo need dispatch this events 'cellEditingStarted'
// this.params - from agInit
// !!! NOT this.params.api.startEditingCell

this.params.api.dispatchEvent({
    ...this.params,
    type: 'cellEditingStarted',
});
    
// change the value cell    
this.params.node.setDataValue(this.field, nextValue);
        
// dispatch 'cellEditingStopped'
// !!! NOT this.params.api.stopEditing()

this.params.api.dispatchEvent({
    ...this.params,
    type: 'cellEditingStopped',
    valueChanged: true,
} as any);

after cellEditingStopped undo/redo ag service push change to the stack