Skip to content

Commit 85eabd2

Browse files
committed
Merge pull request #1557 from PaulL1/cellNav_scrollTo_updated_again
scrollTo method completed on cellNav feature
2 parents f9ec46b + f93b3f4 commit 85eabd2

File tree

3 files changed

+136
-13
lines changed

3 files changed

+136
-13
lines changed

misc/tutorial/202_cellnav.ngdoc

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
@name Tutorial: 202 Cell Navigation
33
@description
44

5-
This grid example uses the ui-grid-cellNav directive to add cell navigation. To enable, you must include the 'ui.grid.cellNav' module and you must include the ui-grid-cellNav directive on your grid element.
5+
This grid example uses the ui-grid-cellNav directive to add cell navigation. To enable, you must include the 'ui.grid.cellNav'
6+
module and you must include the ui-grid-cellNav directive on your grid element.
67

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

10+
Also provides an example of requesting a scroll to a specific row or column programatically - useful for
11+
remembering the state of a page and scrolling back to that position when a user returns.
12+
913
@example
1014
<example module="app">
1115
<file name="app.js">
@@ -15,10 +19,15 @@ Uses the gridOptions.onRegisterApi callback to register the on_cellNav event and
1519
$scope.gridOptions = {};
1620

1721
$scope.gridOptions.columnDefs = [
18-
{ name: 'id' },
19-
{ name: 'name'},
20-
{ name: 'age', displayName: 'Age (not focusable)', allowCellFocus : false },
21-
{ name: 'address.city' }
22+
{ name: 'id', width:'10%' },
23+
{ name: 'name', width:'30%' },
24+
{ name: 'age', displayName: 'Age (not focusable)', allowCellFocus : false, width:'15%' },
25+
{ name: 'address.city', width:'25%' },
26+
{ name: 'phone', width:'30%' },
27+
{ name: 'company', width:'25%' },
28+
{ name: 'email', width:'30%' },
29+
{ name: 'balance', width:'20%' },
30+
{ name: 'guid', width:'40%' }
2231
];
2332

2433
$http.get('/data/500_complex.json')
@@ -35,7 +44,11 @@ Uses the gridOptions.onRegisterApi callback to register the on_cellNav event and
3544
if(rowCol !== null) {
3645
$scope.currentFocused = 'Row Id:' + rowCol.row.entity.id + ' col:' + rowCol.col.colDef.name;
3746
}
38-
}
47+
};
48+
49+
$scope.scrollTo = function( rowIndex, colIndex ) {
50+
$scope.gridApi.cellNav.scrollTo( $scope.gridApi.grid, $scope, $scope.gridOptions.data[rowIndex], $scope.gridOptions.columnDefs[colIndex]);
51+
};
3952

4053
$scope.gridOptions.onRegisterApi = function(gridApi){
4154
$scope.gridApi = gridApi;
@@ -53,6 +66,9 @@ Uses the gridOptions.onRegisterApi callback to register the on_cellNav event and
5366
</file>
5467
<file name="index.html">
5568
<div ng-controller="MainCtrl">
69+
<button type="button" class="btn btn-success" ng-click="scrollTo(20,0)">Scroll To Row 20</button>
70+
<button type="button" class="btn btn-success" ng-click="scrollTo(0,7)">Scroll To Balance</button>
71+
<button type="button" class="btn btn-success" ng-click="scrollTo(50,7)">Scroll To Row 50, Balance</button>
5672
<button type="button" class="btn btn-success" ng-click="getCurrentFocus()">Get Current focused cell</button> <span ng-bind="currentFocused"></span>
5773
<br>
5874
<br>

src/features/cellnav/js/cellnav.js

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@
6767
* @ngdoc function
6868
* @name scrollTo
6969
* @methodOf ui.grid.cellNav.api:PublicApi
70-
* @description (TODO) brings the row and column into view
70+
* @description brings the specified row and column into view
71+
* @param {Grid} grid the grid you'd like to act upon, usually available
72+
* from gridApi.grid
73+
* @param {object} $scope a scope we can broadcast events from
7174
* @param {object} rowEntity gridOptions.data[] array instance to make visible
7275
* @param {object} colDef to make visible
7376
*/
74-
scrollTo: function (rowEntity, colDef) {
75-
var row = grid.getRow(rowEntity);
76-
if (row !== null) {
77-
//todo: scroll into view
78-
}
77+
scrollTo: function (grid, $scope, rowEntity, colDef) {
78+
service.scrollTo(grid, $scope, rowEntity, colDef);
7979
},
8080
/**
8181
* @ngdoc function
@@ -285,6 +285,64 @@
285285
colDef.allowCellFocus = colDef.allowCellFocus === undefined ? true : colDef.allowCellFocus ;
286286

287287
return $q.all(promises);
288+
},
289+
290+
/**
291+
* @ngdoc method
292+
* @methodOf ui.grid.cellNav.service:uiGridCellNavService
293+
* @name scrollVerticallyTo
294+
* @description Scroll the grid vertically such that the specified
295+
* row is in view
296+
* @param {Grid} grid the grid you'd like to act upon, usually available
297+
* from gridApi.grid
298+
* @param {object} $scope a scope we can broadcast events from
299+
* @param {object} rowEntity gridOptions.data[] array instance to make visible
300+
* @param {object} colDef to make visible
301+
*/
302+
scrollTo: function (grid, $scope, rowEntity, colDef) {
303+
var args = {};
304+
305+
if ( rowEntity !== null ){
306+
var row = grid.getRow(rowEntity);
307+
if ( row ) {
308+
args.y = { percentage: row.index / grid.renderContainers.body.visibleRowCache.length };
309+
}
310+
}
311+
312+
if ( colDef !== null ){
313+
var col = grid.getColumn(colDef.name ? colDef.name : colDef.field);
314+
if ( col ) {
315+
args.x = { percentage: this.getLeftWidth(grid, col.index) / this.getLeftWidth(grid, grid.renderContainers.body.visibleColumnCache.length - 1) };
316+
}
317+
}
318+
319+
if ( args.y || args.x ){
320+
$scope.$broadcast(uiGridConstants.events.GRID_SCROLL, args);
321+
}
322+
},
323+
324+
325+
/**
326+
* @ngdoc method
327+
* @methodOf ui.grid.cellNav.service:uiGridCellNavService
328+
* @name getLeftWidth
329+
* @description Get the current drawn width of the columns in the
330+
* grid up to and including the numbered column
331+
* @param {Grid} grid the grid you'd like to act upon, usually available
332+
* from gridApi.grid
333+
* @param {object} colIndex the column to total up to and including
334+
*/
335+
getLeftWidth: function( grid, colIndex ){
336+
var width = 0;
337+
338+
if ( !colIndex ){ return; }
339+
340+
for ( var i=0; i <= colIndex; i++ ){
341+
if ( grid.renderContainers.body.visibleColumnCache[i].drawnWidth ){
342+
width += grid.renderContainers.body.visibleColumnCache[i].drawnWidth;
343+
}
344+
}
345+
return width;
288346
}
289347

290348
};

src/features/cellnav/test/uiGridCellNavService.spec.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ describe('ui.grid.edit uiGridCellNavService', function () {
44
var grid;
55
var uiGridConstants;
66
var uiGridCellNavConstants;
7+
var $rootScope;
78

89
beforeEach(module('ui.grid.cellNav'));
910

10-
beforeEach(inject(function (_uiGridCellNavService_, _gridClassFactory_, $templateCache, _uiGridConstants_, _uiGridCellNavConstants_) {
11+
beforeEach(inject(function (_uiGridCellNavService_, _gridClassFactory_, $templateCache, _uiGridConstants_, _uiGridCellNavConstants_, _$rootScope_) {
1112
uiGridCellNavService = _uiGridCellNavService_;
1213
gridClassFactory = _gridClassFactory_;
1314
uiGridConstants = _uiGridConstants_;
1415
uiGridCellNavConstants = _uiGridCellNavConstants_;
16+
$rootScope = _$rootScope_;
1517

1618
$templateCache.put('ui-grid/uiGridCell', '<div/>');
1719

@@ -291,4 +293,51 @@ describe('ui.grid.edit uiGridCellNavService', function () {
291293

292294
});
293295

296+
describe('scrollTo', function () {
297+
var evt;
298+
var args;
299+
var $scope;
300+
301+
beforeEach(function(){
302+
grid.registerColumnBuilder(uiGridCellNavService.cellNavColumnBuilder);
303+
grid.buildColumns();
304+
grid.setVisibleColumns(grid.columns);
305+
grid.setVisibleRows(grid.rows);
306+
$scope = $rootScope.$new();
307+
308+
evt = null;
309+
args = null;
310+
$scope.$on(uiGridConstants.events.GRID_SCROLL, function( receivedEvt, receivedArgs ){
311+
evt = receivedEvt;
312+
args = receivedArgs;
313+
});
314+
grid.columns[0].drawnWidth = 100;
315+
grid.columns[1].drawnWidth = 200;
316+
grid.columns[2].drawnWidth = 300;
317+
});
318+
319+
it('should request scroll to row and column', function () {
320+
uiGridCellNavService.scrollTo( grid, $scope, grid.options.data[2], grid.columns[1].colDef);
321+
322+
expect(args).toEqual( { y : { percentage : 2/3 }, x : { percentage : 300/600 } });
323+
});
324+
325+
it('should request scroll to row only', function () {
326+
uiGridCellNavService.scrollTo( grid, $scope, grid.options.data[2], null);
327+
328+
expect(args).toEqual( { y : { percentage : 2/3 } });
329+
});
330+
331+
it('should request scroll to column only', function () {
332+
uiGridCellNavService.scrollTo( grid, $scope, null, grid.columns[1].colDef);
333+
334+
expect(args).toEqual( { x : { percentage : 300/600 } });
335+
});
336+
337+
it('should request no scroll as no row or column', function () {
338+
uiGridCellNavService.scrollTo( grid, $scope, null, null );
339+
340+
expect(args).toEqual(null);
341+
});
342+
});
294343
});

0 commit comments

Comments
 (0)