Skip to content

Commit

Permalink
Fixed pagination issue #138
Browse files Browse the repository at this point in the history
  • Loading branch information
Zizzamia committed Jun 3, 2015
1 parent f8e4bc3 commit 74806b8
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 13 deletions.
18 changes: 10 additions & 8 deletions src/component/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@ angular.module('ngTasty.component.table', [

setCount = function(count) {
var maxItems, page;
scope.itemsPerPage = count;
maxItems = count * scope.pagination.page;
if (maxItems > scope.pagination.size) {
page = Math.ceil(scope.pagination.size / count);
Expand All @@ -712,20 +713,22 @@ angular.module('ngTasty.component.table', [
return false;
}
scope.pagMaxRange = scope.pagMinRange;
scope.pagMinRange = scope.pagMaxRange - scope.itemsPerPage;
scope.pagMinRange = scope.pagMaxRange - 5;
setPaginationRanges();
};

setRemainingRange = function () {
if (scope.pagHideMaxRange === true || scope.pagMaxRange > scope.pagination.pages) {
if (scope.pagHideMaxRange === true ||
scope.pagMaxRange > scope.pagination.pages) {
return false;
}
scope.pagMinRange = scope.pagMaxRange;
scope.pagMaxRange = scope.pagMinRange + scope.itemsPerPage;
if (scope.pagMaxRange > scope.pagination.pages) {
scope.pagMaxRange = scope.pagination.pages;
scope.pagMaxRange = scope.pagMinRange + 5;
if (scope.pagMaxRange >= scope.pagination.pages) {
scope.pagMaxRange = scope.pagination.pages + 1;
scope.pagMinRange = scope.pagMaxRange - 5 + 1;
}
scope.pagMinRange = scope.pagMaxRange - scope.itemsPerPage;
scope.pagMinRange = scope.pagMaxRange - 5;
setPaginationRanges();
};

Expand All @@ -737,7 +740,7 @@ angular.module('ngTasty.component.table', [
scope.pagMaxRange = scope.pagination.pages + 1;
}
scope.pagHideMinRange = scope.pagMinRange <= 1;
scope.pagHideMaxRange = scope.pagMaxRange >= scope.pagination.pages;
scope.pagHideMaxRange = scope.pagMaxRange > scope.pagination.pages;
scope.classPageMinRange = scope.pagHideMinRange ? 'disabled' : '';
scope.classPageMaxRange = scope.pagHideMaxRange ? 'disabled' : '';

Expand All @@ -747,7 +750,6 @@ angular.module('ngTasty.component.table', [
break;
}
}

scope.rangePage = $filter('range')([], scope.pagMinRange, scope.pagMaxRange);

if (!tastyTable.start) {
Expand Down
6 changes: 3 additions & 3 deletions src/component/test/table-server-side.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,8 @@ describe('Component: table server side', function () {
expect(tastyPagination.isolateScope().pagMinRange).toEqual(1);
expect(tastyPagination.isolateScope().pagMaxRange).toEqual(6);
tastyPagination.isolateScope().page.remaining();
expect(tastyPagination.isolateScope().pagMinRange).toEqual(2);
expect(tastyPagination.isolateScope().pagMaxRange).toEqual(7);
expect(tastyPagination.isolateScope().pagMinRange).toEqual(3);
expect(tastyPagination.isolateScope().pagMaxRange).toEqual(8);
tastyPagination.isolateScope().page.previous();
expect(tastyPagination.isolateScope().pagMinRange).toEqual(1);
expect(tastyPagination.isolateScope().pagMaxRange).toEqual(6);
Expand All @@ -510,7 +510,7 @@ describe('Component: table server side', function () {
tastyPagination.isolateScope().page.previous();
expect(tastyPagination.isolateScope().rangePage).toEqual([1,2,3,4,5]);
tastyPagination.isolateScope().page.remaining();
expect(tastyPagination.isolateScope().rangePage).toEqual([2,3,4,5,6]);
expect(tastyPagination.isolateScope().rangePage).toEqual([3,4,5,6,7]);
tastyPagination.isolateScope().page.previous();
expect(tastyPagination.isolateScope().rangePage).toEqual([1,2,3,4,5]);
});
Expand Down
71 changes: 71 additions & 0 deletions src/component/test/table.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,77 @@ describe('Component: table', function () {
});


describe('with pagination count 1', function () {
beforeEach(inject(function ($rootScope, $compile, _sortingJSON_) {
$scope = $rootScope.$new();
$scope.resource = angular.copy(_sortingJSON_);
$scope.itemsPerPage = 1;
$scope.listItemsPerPage = [1, 10, 20, 40, 80];
element = angular.element(''+
'<div tasty-table bind-resource="resource">'+
' <table>'+
' <thead>'+
' <tr>'+
' <th>Name</th>'+
' <th>Star</th>'+
' <th>SF Location</th>'+
' </tr>'+
' </thead>'+
' <tbody>'+
' <tr ng-repeat="row in rows">'+
' <td>{{ row.name }}</td>'+
' <td>{{ row.star }}</td>'+
' <td>{{ row[\'sf-location\'] }}</td>'+
' </tr>'+
' </tbody>'+
' </table>'+
' <tasty-pagination bind-items-per-page="itemsPerPage" '+
' bind-list-items-per-page="listItemsPerPage"></tasty-pagination>'+
'</div>');
tastyTable = $compile(element)($scope);
tastyPagination = tastyTable.find('tasty-pagination');
$scope.$digest();
}));

it('should update pagMinRange and pagMaxRange when page.previous and page.remaining are clicked', function () {
expect(tastyPagination.isolateScope().pagMinRange).toEqual(1);
expect(tastyPagination.isolateScope().pagMaxRange).toEqual(6);
tastyPagination.isolateScope().page.previous();
expect(tastyPagination.isolateScope().pagMinRange).toEqual(1);
expect(tastyPagination.isolateScope().pagMaxRange).toEqual(6);
tastyPagination.isolateScope().page.remaining();
expect(tastyPagination.isolateScope().pagMinRange).toEqual(6);
expect(tastyPagination.isolateScope().pagMaxRange).toEqual(11);
tastyPagination.isolateScope().page.remaining();
expect(tastyPagination.isolateScope().pagMinRange).toEqual(11);
expect(tastyPagination.isolateScope().pagMaxRange).toEqual(16);
tastyPagination.isolateScope().page.remaining();
expect(tastyPagination.isolateScope().pagMinRange).toEqual(16);
expect(tastyPagination.isolateScope().pagMaxRange).toEqual(21);
tastyPagination.isolateScope().page.previous();
expect(tastyPagination.isolateScope().pagMinRange).toEqual(11);
expect(tastyPagination.isolateScope().pagMaxRange).toEqual(16);
tastyPagination.isolateScope().page.previous();
expect(tastyPagination.isolateScope().pagMinRange).toEqual(6);
expect(tastyPagination.isolateScope().pagMaxRange).toEqual(11);
});

it('should update rangePage when page.previous and page.remaining are clicked', function () {
expect(tastyPagination.isolateScope().rangePage).toEqual([1,2,3,4,5]);
tastyPagination.isolateScope().page.previous();
expect(tastyPagination.isolateScope().rangePage).toEqual([1,2,3,4,5]);
tastyPagination.isolateScope().page.remaining();
expect(tastyPagination.isolateScope().rangePage).toEqual([6,7,8,9,10]);
tastyPagination.isolateScope().page.remaining();
expect(tastyPagination.isolateScope().rangePage).toEqual([11,12,13,14,15]);
tastyPagination.isolateScope().page.previous();
expect(tastyPagination.isolateScope().rangePage).toEqual([6,7,8,9,10]);
tastyPagination.isolateScope().page.previous();
expect(tastyPagination.isolateScope().rangePage).toEqual([1,2,3,4,5]);
});
});


describe('with pagination classic binding', function () {
beforeEach(inject(function ($rootScope, $compile, _sortingJSON_) {
$scope = $rootScope.$new();
Expand Down
4 changes: 2 additions & 2 deletions website/static/js/pages/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ angular.module('myApp.pages.table', [])
'page': 2
}
};
$scope.itemsPerPage = 20;
$scope.listItemsPerPage = [10, 20, 40, 80];
$scope.itemsPerPage = 10;
$scope.listItemsPerPage = [1, 10, 40, 80];
$timeout(function () {
Rainbow.color();
});
Expand Down

0 comments on commit 74806b8

Please sign in to comment.