Skip to content

Commit 3bcbe72

Browse files
committed
fix(Grid): Force scroll to 100% when necessary
Containers that have overflow: hidden set are not able to naturally scroll to 100%, because they cannot handle native scroll events; only mousewheel. It's a long discussion; see #3772 for notes. This change catches a mousewheel event that should put the container at the max scroll position (basically scrollHeight - offsetHeight) and manually sets the scrollTop to that amount. Fixes #3772
1 parent 6a69120 commit 3bcbe72

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

misc/demo/pinning.html

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css" />
1010
<link href="/dist/release/ui-grid.css" rel="stylesheet">
1111

12-
<!--<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>-->
12+
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
1313
<script src="/lib/test/angular/1.2.26/angular.js"></script>
1414
<script src="/dist/release/ui-grid.js"></script>
1515

@@ -45,7 +45,9 @@ <h2>Grid</h2>
4545
<script>
4646
var app = angular.module('test', ['ui.grid', 'ui.grid.pinning', 'ui.grid.resizeColumns']);
4747
app.controller('Main', function($scope, $http) {
48-
$scope.gridOptions = {};
48+
$scope.gridOptions = {
49+
rowHeight: 45
50+
};
4951
$scope.gridOptions.columnDefs = [
5052
{ name:'id', width:50 },
5153
{ name:'name', width:100, pinnedLeft:true },
@@ -65,7 +67,7 @@ <h2>Grid</h2>
6567

6668
$http.get('https://rawgit.com/angular-ui/ui-grid.info/gh-pages/data/500_complex.json')
6769
.success(function(data) {
68-
$scope.gridOptions.data = data;
70+
$scope.gridOptions.data = data.slice(0, 50);
6971
});
7072
});
7173
</script>

src/js/core/directives/ui-grid-render-container.js

+7
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,17 @@
7474
var scrollYAmount = event.deltaY * -1 * event.deltaFactor;
7575

7676
scrollTop = containerCtrl.viewport[0].scrollTop;
77+
7778
// Get the scroll percentage
7879
scrollEvent.verticalScrollLength = rowContainer.getVerticalScrollLength();
7980
var scrollYPercentage = (scrollTop + scrollYAmount) / scrollEvent.verticalScrollLength;
8081

82+
// If we should be scrolled 100%, make sure the scrollTop matches the maximum scroll length
83+
// Viewports that have "overflow: hidden" don't let the mousewheel scroll all the way to the bottom without this check
84+
if (scrollYPercentage >= 1 && scrollTop < scrollEvent.verticalScrollLength) {
85+
containerCtrl.viewport[0].scrollTop = scrollEvent.verticalScrollLength;
86+
}
87+
8188
// Keep scrollPercentage within the range 0-1.
8289
if (scrollYPercentage < 0) { scrollYPercentage = 0; }
8390
else if (scrollYPercentage > 1) { scrollYPercentage = 1; }

src/js/core/directives/ui-grid-viewport.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
$scope.rowContainer = containerCtrl.rowContainer;
2929
$scope.colContainer = containerCtrl.colContainer;
3030

31-
// Register this viewport with its container
31+
// Register this viewport with its container
3232
containerCtrl.viewport = $elm;
3333

3434

@@ -138,4 +138,4 @@
138138
}
139139
]);
140140

141-
})();
141+
})();

src/js/core/factories/GridRenderContainer.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ angular.module('ui.grid')
300300
};
301301

302302
GridRenderContainer.prototype.getVerticalScrollLength = function getVerticalScrollLength() {
303-
return this.getCanvasHeight() - this.getViewportHeight();
303+
return this.getCanvasHeight() - this.getViewportHeight() + this.grid.scrollbarHeight;
304304
};
305305

306306
GridRenderContainer.prototype.getCanvasWidth = function getCanvasWidth() {
@@ -354,6 +354,8 @@ angular.module('ui.grid')
354354

355355
vertScrollPercentage = newScrollTop / vertScrollLength;
356356

357+
// console.log('vert', vertScrollPercentage, newScrollTop, vertScrollLength);
358+
357359
if (vertScrollPercentage > 1) { vertScrollPercentage = 1; }
358360
if (vertScrollPercentage < 0) { vertScrollPercentage = 0; }
359361

@@ -429,13 +431,17 @@ angular.module('ui.grid')
429431

430432
var maxRowIndex = rowCache.length - minRows;
431433

434+
// console.log('scroll%1', scrollPercentage);
435+
432436
// Calculate the scroll percentage according to the scrollTop location, if no percentage was provided
433437
if ((typeof(scrollPercentage) === 'undefined' || scrollPercentage === null) && scrollTop) {
434438
scrollPercentage = scrollTop / self.getVerticalScrollLength();
435439
}
436440

437441
var rowIndex = Math.ceil(Math.min(maxRowIndex, maxRowIndex * scrollPercentage));
438442

443+
// console.log('maxRowIndex / scroll%', maxRowIndex, scrollPercentage, rowIndex);
444+
439445
// Define a max row index that we can't scroll past
440446
if (rowIndex > maxRowIndex) {
441447
rowIndex = maxRowIndex;
@@ -738,7 +744,7 @@ angular.module('ui.grid')
738744
GridRenderContainer.prototype.getViewportStyle = function () {
739745
var self = this;
740746
var styles = {};
741-
747+
742748
self.hasHScrollbar = false;
743749
self.hasVScrollbar = false;
744750

0 commit comments

Comments
 (0)