ui-grid icon indicating copy to clipboard operation
ui-grid copied to clipboard

afterCellEdit event always fired twice, once on edit and once on scroll

Open ankit5990 opened this issue 12 years ago • 7 comments

afterCellEdit event is always being fired twice. Once when actual editing of a cell is done, and other when a scroll is done on the grid.

When doing a server side save, this is leading to useless AJAX calls.

Made a plunk to show the issue.

  1. select gender from the dropdown.
  2. an alert shows afterCellEdit event.
  3. scroll on the grid.
  4. An alert is again seen for afterCellEdit event.

http://embed.plnkr.co/UZjzKbFqd4pLkc35zrzE/preview

ankit5990 avatar Oct 07 '14 13:10 ankit5990

Is this a duplicate of #1520?

PaulL1 avatar Oct 08 '14 01:10 PaulL1

I dont think so. It does not seem like a debounce issue. We always get two callbacks. And second is always seen on scroll.

ankit5990 avatar Oct 08 '14 05:10 ankit5990

I'm having the same issue. Seems that after flushing the dirty rows and then scrolling the grid, they are dirty again.

Can be reproduced with this plunker (90% of the times):

  1. Change the gender cell value male -> female
  2. Flush dirty (dirty rows are shown until promise is resolved). Grid seems to be ok
  3. Then scroll the grid. Same row is still dirty
  4. Flush dirty again, wait until promise is resolved
  5. The grid is clean.

http://plnkr.co/edit/DdMGLf9b23XKRGNHGgpu?p=preview

[EDIT] Shouldn't the edit.raise.afterCellEdit called only once, not twice as in below.

//stop editing when grid is scrolled
var deregOnGridScroll = $scope.col.grid.api.core.on.scrollEvent($scope, function () {
  endEdit(true);
  $scope.grid.api.edit.raise.afterCellEdit($scope.row.entity, $scope.col.colDef, cellModel($scope), origCellValue);
  deregOnGridScroll();
});

//end editing
var deregOnEndCellEdit = $scope.$on(uiGridEditConstants.events.END_CELL_EDIT, function (evt, retainFocus) {
  endEdit(retainFocus);
  $scope.grid.api.edit.raise.afterCellEdit($scope.row.entity, $scope.col.colDef, cellModel($scope), origCellValue);
  deregOnEndCellEdit();
});

Maybe something like:

var dereg = true;

//stop editing when grid is scrolled
var deregOnGridScroll = $scope.col.grid.api.core.on.scrollEvent($scope, function () {
  if (dereg){
    endEdit(true);
    console.log('scroll', cellModel($scope), origCellValue);
    $scope.grid.api.edit.raise.afterCellEdit($scope.row.entity, $scope.col.colDef, cellModel($scope), origCellValue);
    deregOnGridScroll();
  }

  dereg = false;
});

//end editing
var deregOnEndCellEdit = $scope.$on(uiGridEditConstants.events.END_CELL_EDIT, function (evt, retainFocus) {
  if (dereg){
    endEdit(retainFocus);
    console.log('blur', cellModel($scope), origCellValue);
    $scope.grid.api.edit.raise.afterCellEdit($scope.row.entity, $scope.col.colDef, cellModel($scope), origCellValue);
    deregOnEndCellEdit();
  }

  dereg = false;
});

mtlehtin avatar Jan 21 '15 06:01 mtlehtin

OK, from debugging through the code, it looks like we register a listener for ending cell edit, and another for grid scroll. When we end cell edit we don't deregister the scroll listener, so when we scroll it still fires. We need to deregister both listeners when either are triggered. I'll do an update.

PaulL1 avatar Jan 21 '15 07:01 PaulL1

Closed in #2600

PaulL1 avatar Jan 21 '15 08:01 PaulL1

Thanks a lot!

mtlehtin avatar Jan 21 '15 08:01 mtlehtin

Hi,

We are using the editable grid where user can navigate through cells via tabbing and edit the cell. Things work as expected till we have horizontal scroll either not present on the screen OR scroller has not been moved forward from its initial zero position.

Ex: say while tabbing till cell 6 horizontal scroller remains stable, so till that cell user can tab, focus and edit the cell. The moment we move to cell 7 and scroller moves forward, user can tab to cell 7, focus works on cell 7 too but cell becomes non editable, reason being the call to afterCellEdit event in deregOnGridScroll section (beginEditAfterScroll function) present in ui-grid-edit.js. User can surely edit the cell by clicking on it OR scrolling the horizontal bar, but it goes against the tabbing/focusing/editing feature of ui-grid and defeats the purpose. Could you guys help me here, so that user can keep on editing the cell from 1st column till the last one by tabbing through cells with scroller moving forward?

piyush21upadhyay avatar Jun 01 '20 14:06 piyush21upadhyay