Skip to content

Commit

Permalink
fix(Grid.js): scrollToIfNecessary can scroll to last row.
Browse files Browse the repository at this point in the history
Updated the scrollToIfNecessary to treat any percentage number for the scroll position that is
greater than 1 as 1.

fix #6571, fix #6507, fix #6563
  • Loading branch information
Portugal, Marcelo authored and mportuga committed Mar 28, 2018
1 parent 842a9b7 commit 87c9eed
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
1 change: 1 addition & 0 deletions misc/tutorial/202_cellnav.ngdoc
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ For better performance with the following example, you can choose to load the ui
<button type="button" class="btn btn-success" ng-click="$ctrl.scrollTo(20,0)">Scroll To Row 20</button>
<button type="button" class="btn btn-success" ng-click="$ctrl.scrollTo(0,7)">Scroll To Balance</button>
<button type="button" class="btn btn-success" ng-click="$ctrl.scrollTo(50,7)">Scroll To Row 50, Balance</button>
<button type="button" class="btn btn-success" ng-click="$ctrl.scrollTo(499,7)">Scroll To Row 499, Balance</button>
<button type="button" class="btn btn-success" ng-click="$ctrl.scrollToFocus(50,7)">Focus Row 50, Balance</button>
<br>
<br>
Expand Down
22 changes: 11 additions & 11 deletions src/js/core/factories/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -2339,6 +2339,14 @@ angular.module('ui.grid')
return this.hasRightContainer() && this.renderContainers.right.renderedColumns.length > 0;
};

// Turn the scroll position into a percentage and make it an argument for a scroll event
function getScrollPercentage(scrollPixels, scrollLength) {
var percentage = scrollPixels / scrollLength;

// if the percentage is greater than 1, set it to 1
return percentage <= 1 ? percentage : 1;
}

/**
* @ngdoc method
* @methodOf ui.grid.class:Grid
Expand Down Expand Up @@ -2405,31 +2413,23 @@ angular.module('ui.grid')
// Don't let the pixels required to see the row be less than zero
pixelsToSeeRow = (pixelsToSeeRow < 0) ? 0 : pixelsToSeeRow;

var scrollPixels, percentage;
var scrollPixels;

// If the scroll position we need to see the row is LESS than the top boundary, i.e. obscured above the top of the self...
if (pixelsToSeeRow < topBound) {
// Get the different between the top boundary and the required scroll position and subtract it from the current scroll position\
// to get the full position we need
scrollPixels = self.renderContainers.body.prevScrollTop - (topBound - pixelsToSeeRow);

// Turn the scroll position into a percentage and make it an argument for a scroll event
percentage = scrollPixels / scrollLength;
if (percentage <= 1) {
scrollEvent.y = { percentage: percentage };
}
scrollEvent.y = { percentage: getScrollPercentage(scrollPixels, scrollLength) };
}
// Otherwise if the scroll position we need to see the row is MORE than the bottom boundary, i.e. obscured below the bottom of the self...
else if (pixelsToSeeRow > bottomBound) {
// Get the different between the bottom boundary and the required scroll position and add it to the current scroll position
// to get the full position we need
scrollPixels = pixelsToSeeRow - bottomBound + self.renderContainers.body.prevScrollTop;

// Turn the scroll position into a percentage and make it an argument for a scroll event
percentage = scrollPixels / scrollLength;
if (percentage <= 1) {
scrollEvent.y = { percentage: percentage };
}
scrollEvent.y = { percentage: getScrollPercentage(scrollPixels, scrollLength) };
}
}

Expand Down

0 comments on commit 87c9eed

Please sign in to comment.