Skip to content

Commit f113977

Browse files
committed
Merge pull request #2376 from PaulL1/2221_scrollToFocus
Fix #2221 (cellNav): Add scrollToFocus method
2 parents 23d83de + 1194558 commit f113977

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

misc/tutorial/202_cellnav.ngdoc

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ module and you must include the ui-grid-cellNav directive on your grid element.
77

88
Uses the gridOptions.onRegisterApi callback to register the on_cellNav event and log when the cell is navigated.
99

10-
Also provides an example of requesting a scroll to a specific row or column programatically - useful for
10+
Provides an example of requesting a scroll to a specific row or column programatically - useful for
1111
remembering the state of a page and scrolling back to that position when a user returns.
1212

13+
Provides an example of scrolling to and setting the focus on a specific cell, using the scrollToFocus api.
14+
1315
@example
1416
<example module="app">
1517
<file name="app.js">
@@ -50,6 +52,10 @@ remembering the state of a page and scrolling back to that position when a user
5052
$scope.gridApi.cellNav.scrollTo( $scope.gridApi.grid, $scope, $scope.gridOptions.data[rowIndex], $scope.gridOptions.columnDefs[colIndex]);
5153
};
5254

55+
$scope.scrollToFocus = function( rowIndex, colIndex ) {
56+
$scope.gridApi.cellNav.scrollToFocus( $scope.gridApi.grid, $scope, $scope.gridOptions.data[rowIndex], $scope.gridOptions.columnDefs[colIndex]);
57+
};
58+
5359
$scope.gridOptions.onRegisterApi = function(gridApi){
5460
$scope.gridApi = gridApi;
5561
gridApi.cellNav.on.navigate($scope,function(newRowCol, oldRowCol){
@@ -69,6 +75,7 @@ remembering the state of a page and scrolling back to that position when a user
6975
<button type="button" class="btn btn-success" ng-click="scrollTo(20,0)">Scroll To Row 20</button>
7076
<button type="button" class="btn btn-success" ng-click="scrollTo(0,7)">Scroll To Balance</button>
7177
<button type="button" class="btn btn-success" ng-click="scrollTo(50,7)">Scroll To Row 50, Balance</button>
78+
<button type="button" class="btn btn-success" ng-click="scrollToFocus(50,7)">Focus Row 50, Balance</button>
7279
<button type="button" class="btn btn-success" ng-click="getCurrentFocus()">Get Current focused cell</button> <span ng-bind="currentFocused"></span>
7380
<br>
7481
<br>

src/features/cellnav/js/cellnav.js

+50-2
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,22 @@
218218
service.scrollTo(grid, $scope, rowEntity, colDef);
219219
},
220220

221+
/**
222+
* @ngdoc function
223+
* @name scrollToFocus
224+
* @methodOf ui.grid.cellNav.api:PublicApi
225+
* @description brings the specified row and column into view, and sets focus
226+
* to that cell
227+
* @param {Grid} grid the grid you'd like to act upon, usually available
228+
* from gridApi.grid
229+
* @param {object} $scope a scope we can broadcast events from
230+
* @param {object} rowEntity gridOptions.data[] array instance to make visible and set focus
231+
* @param {object} colDef to make visible and set focus
232+
*/
233+
scrollToFocus: function (grid, $scope, rowEntity, colDef) {
234+
service.scrollToFocus(grid, $scope, rowEntity, colDef);
235+
},
236+
221237
/**
222238
* @ngdoc function
223239
* @name getFocusedCell
@@ -344,6 +360,37 @@
344360
this.scrollToInternal(grid, $scope, gridRow, gridCol);
345361
},
346362

363+
/**
364+
* @ngdoc method
365+
* @methodOf ui.grid.cellNav.service:uiGridCellNavService
366+
* @name scrollToFocus
367+
* @description Scroll the grid such that the specified
368+
* row and column is in view, and set focus to the cell in that row and column
369+
* @param {Grid} grid the grid you'd like to act upon, usually available
370+
* from gridApi.grid
371+
* @param {object} $scope a scope we can broadcast events from
372+
* @param {object} rowEntity gridOptions.data[] array instance to make visible and set focus to
373+
* @param {object} colDef to make visible and set focus to
374+
*/
375+
scrollToFocus: function (grid, $scope, rowEntity, colDef) {
376+
var gridRow = null, gridCol = null;
377+
378+
if (rowEntity !== null) {
379+
gridRow = grid.getRow(rowEntity);
380+
}
381+
382+
if (colDef !== null) {
383+
gridCol = grid.getColumn(colDef.name ? colDef.name : colDef.field);
384+
}
385+
this.scrollToInternal(grid, $scope, gridRow, gridCol);
386+
387+
var rowCol = { row: gridRow, col: gridCol };
388+
389+
// Broadcast the navigation
390+
grid.cellNav.broadcastCellNav(rowCol);
391+
392+
},
393+
347394
/**
348395
* @ngdoc method
349396
* @methodOf ui.grid.cellNav.service:uiGridCellNavService
@@ -626,6 +673,7 @@
626673
compile: function () {
627674
return {
628675
pre: function ($scope, $elm, $attrs, uiGridCtrl) {
676+
var _scope = $scope;
629677

630678
var grid = uiGridCtrl.grid;
631679
uiGridCellNavService.initializeGrid(grid);
@@ -637,8 +685,8 @@
637685
};
638686

639687
// gridUtil.logDebug('uiGridEdit preLink');
640-
uiGridCtrl.cellNav.broadcastCellNav = function (newRowCol) {
641-
$scope.$broadcast(uiGridCellNavConstants.CELL_NAV_EVENT, newRowCol);
688+
uiGridCtrl.cellNav.broadcastCellNav = grid.cellNav.broadcastCellNav = function (newRowCol) {
689+
_scope.$broadcast(uiGridCellNavConstants.CELL_NAV_EVENT, newRowCol);
642690
uiGridCtrl.cellNav.broadcastFocus(newRowCol);
643691
};
644692

0 commit comments

Comments
 (0)