diff --git a/src/features/cellnav/js/cellnav.js b/src/features/cellnav/js/cellnav.js index 392fb5d7c3..73a04901ab 100644 --- a/src/features/cellnav/js/cellnav.js +++ b/src/features/cellnav/js/cellnav.js @@ -1067,6 +1067,10 @@ }); } + // In case we created a new row, and we are the new created row by ngRepeat + // then this cell content might have been selected previously + refreshCellFocus(); + function preventMouseDown(evt) { //Prevents the foucus event from firing if the click event is already going to fire. //If both events fire it will cause bouncing behavior. @@ -1081,16 +1085,27 @@ }); // This event is fired for all cells. If the cell matches, then focus is set - $scope.$on(uiGridCellNavConstants.CELL_NAV_EVENT, function (evt, rowCol, modifierDown) { - var isFocused = grid.cellNav.focusedCells.some(function(focusedRowCol, index){ + $scope.$on(uiGridCellNavConstants.CELL_NAV_EVENT, refreshCellFocus); + + // Refresh cell focus when a new row id added to the grid + var dataChangeDereg = uiGridCtrl.grid.registerDataChangeCallback(function (grid) { + // Clear the focus if it's set to avoid the wrong cell getting focused during + // a short period of time (from now until $timeout function executed) + clearFocus(); + + $timeout(refreshCellFocus); + }, [uiGridConstants.dataChange.ROW]); + + function refreshCellFocus() { + var isFocused = grid.cellNav.focusedCells.some(function (focusedRowCol, index) { return (focusedRowCol.row === $scope.row && focusedRowCol.col === $scope.col); }); - if (isFocused){ + if (isFocused) { setFocused(); } else { clearFocus(); } - }); + } function setFocused() { if (!$scope.focused){ @@ -1113,6 +1128,8 @@ } $scope.$on('$destroy', function () { + dataChangeDereg(); + //.off withouth paramaters removes all handlers $elm.find('div').off(); $elm.off();