Skip to content

Commit e6bc300

Browse files
committed
fix(edit): fixes #4129 by only adding edit listener events when needed
because of 'track by' in row ng-repeat, neither the scope or dom is destroyed when scolling vertically. This makes attaching events to Dom when row changes a bit more complex
1 parent 9aea44a commit e6bc300

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

src/features/edit/js/gridEdit.js

+29-20
Original file line numberDiff line numberDiff line change
@@ -483,35 +483,30 @@
483483
}
484484

485485
var cellNavNavigateDereg = function() {};
486+
var viewPortKeyDownDereg = function() {};
486487

487-
// Bind to keydown events in the render container
488-
if (uiGridCtrl && uiGridCtrl.grid.api.cellNav) {
489-
490-
uiGridCtrl.grid.api.cellNav.on.viewPortKeyDown($scope, function (evt, rowCol) {
491-
if (rowCol === null) {
492-
return;
493-
}
494-
495-
if (rowCol.row === $scope.row && rowCol.col === $scope.col && !$scope.col.colDef.enableCellEditOnFocus) {
496-
//important to do this before scrollToIfNecessary
497-
beginEditKeyDown(evt);
498-
// uiGridCtrl.grid.api.core.scrollToIfNecessary(rowCol.row, rowCol.col);
499-
}
500-
501-
});
502-
}
503488

504489
var setEditable = function() {
505490
if ($scope.col.colDef.enableCellEdit && $scope.row.enableCellEdit !== false) {
506-
registerBeginEditEvents();
491+
if (!$scope.beginEditEventsWired) { //prevent multiple attachments
492+
registerBeginEditEvents();
493+
}
507494
} else {
508-
cancelBeginEditEvents();
495+
if ($scope.beginEditEventsWired) {
496+
cancelBeginEditEvents();
497+
}
509498
}
510499
};
511500

512501
setEditable();
513502

514-
var rowWatchDereg = $scope.$watch( 'row', setEditable );
503+
var rowWatchDereg = $scope.$watch('row', function (n, o) {
504+
if (n !== o) {
505+
setEditable();
506+
}
507+
});
508+
509+
515510
$scope.$on( '$destroy', rowWatchDereg );
516511

517512
function registerBeginEditEvents() {
@@ -521,6 +516,18 @@
521516
$elm.on('touchstart', touchStart);
522517

523518
if (uiGridCtrl && uiGridCtrl.grid.api.cellNav) {
519+
520+
viewPortKeyDownDereg = uiGridCtrl.grid.api.cellNav.on.viewPortKeyDown($scope, function (evt, rowCol) {
521+
if (rowCol === null) {
522+
return;
523+
}
524+
525+
if (rowCol.row === $scope.row && rowCol.col === $scope.col && !$scope.col.colDef.enableCellEditOnFocus) {
526+
//important to do this before scrollToIfNecessary
527+
beginEditKeyDown(evt);
528+
}
529+
});
530+
524531
cellNavNavigateDereg = uiGridCtrl.grid.api.cellNav.on.navigate($scope, function (newRowCol, oldRowCol) {
525532
if ($scope.col.colDef.enableCellEditOnFocus) {
526533
if (newRowCol.row === $scope.row && newRowCol.col === $scope.col) {
@@ -532,7 +539,7 @@
532539
});
533540
}
534541

535-
542+
$scope.beginEditEventsWired = true;
536543

537544
}
538545

@@ -569,6 +576,8 @@
569576
$elm.off('keydown', beginEditKeyDown);
570577
$elm.off('touchstart', touchStart);
571578
cellNavNavigateDereg();
579+
viewPortKeyDownDereg();
580+
$scope.beginEditEventsWired = false;
572581
}
573582

574583
function beginEditKeyDown(evt) {

0 commit comments

Comments
 (0)