1
votes

I am using ag-grid/ag-grid-angular to provide an editable grid of data backed by a database. When a user edits a cell I want to be able to post the update to the backend service and if the request is successful update the grid and if not undo the user's changes and show an error.

I have approached this problem from a couple different angles but have yet to find the solution that meets all my requirements and am also curious about what the best practice would be to implement this kind of functionality.

My first thought was to leverage the cellValueChanged event. With this approach I can see the old and new values and then make a call to my service to update the database. If the request is successful then everything is great and works as expected. However, if the request fails for some reason then I need to be able to undo the user's changes. Since I have access to the old value I can easily do something like event.node.setDataValue(event.column, event.oldValue) to revert the user's changes. However, since I am updating the grid again this actually triggers the cellValueChanged event a second time. I have no way of knowing that this is the result of undoing the user's changes so I unnecessarily make a call to my service again to update the data even though the original request was never successful in updating the data.

I have also tried using a custom cell editor to get in between when the user is finished editing a cell and when the grid is actually updated. However, it appears that there is no way to integrate an async method in any of these classes to be able to wait for a response from the server to decide whether or not to actually apply the user's changes. E.g.

isCancelBeforeStart(): boolean {
    this.service.updateData(event.data).subscribe(() => {
      return false;
    }, error => {
      return true;
    });
}

does not work because this method is synchronous and I need to be able to wait for a response from my service before deciding whether to cancel the edit or not.

Is there something I am missing or not taking in to account? Or another way to approach this problem to get my intended functionality? I realize this could be handled much easier with dedicated edit/save buttons but I am ideally looking for an interactive grid that is saving the changes to the backend as the user is making changes and providing feedback in cases where something went wrong.

Any help/feedback is greatly appreciated!

3

3 Answers

1
votes

I understand what you are trying to do, and I think that the best approach is going to be to use a "valueSetter" function on each of your editable columns.

With a valueSetter, the grid's value will not be directly updated - you will have to update your bound data to have it reflected in the grid.

When the valueSetter is called by the grid at the end of the edit, you'll probably want to record the original value somehow, update your bound data (so that the grid will reflect the change), and then kick off the back-end save, and return immediately from the valueSetter function.

(It's important to return immediately from the valueSetter function to keep the grid responsive. Since the valueSetter call from the grid is synchronous, if you try to wait for the server response, you're going to lock up the grid while you're waiting.)

Then, if the back-end update succeeds, there's nothing to do, and if it fails, you can update your bound data to reflect the original value.

With this method, you won't have the problem of listening for the cellValueChanged event.

The one issue that you might have to deal with is what to do if the user changes the cell value, and then changes it again before the first back-end save returns.

0
votes
onCellValueChanged: (event) => {
  if (event.oldValue === event.newValue) {
    return;
  }
  try {
    // apiUpdate(event.data)
  }
  catch {
    event.node.data[event.colDef.Field] = event.oldValue;
    event.node.setDataValue(event.column, event.oldValue);
  }
}

By changing the value back on node.data first, when setDataValue() triggers the change event again, oldValue and newValue are actually the same now and the function returns, avoiding the rather slow infinite loop.

I think it's because you change the data behind the scenes directly without agGrid noticing with node.data = , then make a change that agGrid recognises and rerenders the cell by calling setDataValue. Thereby tricking agGrid into behaving.

0
votes

I would suggest a slightly better approach than StangerString, but to credit him the idea came from his approach. Rather than using a test of the oldValue/newValue and allowing the event to be called twice, you can go around the change detection by doing the following.

event.node.data[event.colDef.field] = event.oldValue;
event.api.refreshCells({ rowNodes: [event.node], columns: [event.column.colId] });

What that does is sets the data directly in the data store used by aggrid, then you tell it to refresh that grid. That will prevent the onCellValueChanged event from having to be called again.

(if you arent using colIds you can use the field or pass the whole column, I think any of them work)