Angular Ag Grid - Has anyone figured out a way to wait for a cell node update to happen and THEN fire a function, like a callback?

Viewed 479

I am utilizing cellChanged.node.setDataValue(fieldChanged, oldValue) inside of the (cellValueChanged) event emitter, and I'm having trouble figuring out how to call a function once the setDataValue function has finished executing. I need to do this to do a check to see if a user has the permission to update a cell.

Here is the full code that checks:

if(this.showPaywallNotification) {

      // Okay, so the budget is above what we allow HOWEVER...
      if(budget > BUDGET_AMOUNT) {

      this.showPaywallNotification = false;
      cellChanged.node.setDataValue(fieldChanged, oldValue)

      // Note: This timeout is in place to prevent the infinite updating bug
      // This is problematic because if the user changes the cells fast enough, they can get around the paywall. If I change the timeout to be smaller, the resulting change triggers the update, which ends up creating an infinite loop of updates.
      setTimeout(() => {
           this.showPaywallNotification = true;
      }, 230)
  }

}

Is there a way I can replace my setTimeout() function with something better that can always ensure the user can't get around my paywall by just updating the cell faster than the timeout can execute?

1 Answers

You don't have to do polling. setDataValue is a not an async function.

Also, onCellValueChanged won't get called again if you call node.setDataValue.

Have a look at this plunk: Cell Editing - Revert to old value. Try updating any Age value to negative.

onCellValueChanged($event) {
  if ($event.colDef.field === 'age' && $event.newValue < 0) {
    // debugger
    $event.node.setDataValue('age', $event.oldValue);
    console.log('value reverted');
  }
}

Let me know if something is not clear, or this is not sufficient.

Related