Skip to content

Commit 1dda0a4

Browse files
authored
Merge pull request #4740 from ahocquet/fix-cellnav-focus
Fix cell focus in cellNav when a new row is added
2 parents d47188b + 38d2037 commit 1dda0a4

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/features/cellnav/js/cellnav.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,10 @@
10691069
});
10701070
}
10711071

1072+
// In case we created a new row, and we are the new created row by ngRepeat
1073+
// then this cell content might have been selected previously
1074+
refreshCellFocus();
1075+
10721076
function preventMouseDown(evt) {
10731077
//Prevents the foucus event from firing if the click event is already going to fire.
10741078
//If both events fire it will cause bouncing behavior.
@@ -1083,16 +1087,27 @@
10831087
});
10841088

10851089
// This event is fired for all cells. If the cell matches, then focus is set
1086-
$scope.$on(uiGridCellNavConstants.CELL_NAV_EVENT, function (evt, rowCol, modifierDown) {
1087-
var isFocused = grid.cellNav.focusedCells.some(function(focusedRowCol, index){
1090+
$scope.$on(uiGridCellNavConstants.CELL_NAV_EVENT, refreshCellFocus);
1091+
1092+
// Refresh cell focus when a new row id added to the grid
1093+
var dataChangeDereg = uiGridCtrl.grid.registerDataChangeCallback(function (grid) {
1094+
// Clear the focus if it's set to avoid the wrong cell getting focused during
1095+
// a short period of time (from now until $timeout function executed)
1096+
clearFocus();
1097+
1098+
$timeout(refreshCellFocus);
1099+
}, [uiGridConstants.dataChange.ROW]);
1100+
1101+
function refreshCellFocus() {
1102+
var isFocused = grid.cellNav.focusedCells.some(function (focusedRowCol, index) {
10881103
return (focusedRowCol.row === $scope.row && focusedRowCol.col === $scope.col);
10891104
});
1090-
if (isFocused){
1105+
if (isFocused) {
10911106
setFocused();
10921107
} else {
10931108
clearFocus();
10941109
}
1095-
});
1110+
}
10961111

10971112
function setFocused() {
10981113
if (!$scope.focused){
@@ -1115,6 +1130,8 @@
11151130
}
11161131

11171132
$scope.$on('$destroy', function () {
1133+
dataChangeDereg();
1134+
11181135
//.off withouth paramaters removes all handlers
11191136
$elm.find('div').off();
11201137
$elm.off();

0 commit comments

Comments
 (0)