Skip to content

Commit

Permalink
fix(pagination): should be empty (0) when filtering an empty dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding-SE committed Jan 21, 2020
1 parent 681cd1b commit 7409832
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ describe('PaginationService', () => {
});

describe('changeItemPerPage method', () => {
it('should be on page 1 when total items is 0', () => {
it('should be on page 0 when total items is 0', () => {
mockGridOption.pagination.totalItems = 0;
service.init(gridStub, dataviewStub, mockGridOption.pagination, mockGridOption.backendServiceApi);
service.changeItemPerPage(30);

expect(service.getCurrentPageNumber()).toBe(1);
expect(service.getCurrentPageNumber()).toBe(0);
expect(service.getCurrentItemPerPageCount()).toBe(30);
});

Expand Down Expand Up @@ -428,16 +428,16 @@ describe('PaginationService', () => {
});

describe('recalculateFromToIndexes method', () => {
it('should recalculate the From/To as 1 when total items is 0', () => {
it('should recalculate the From/To as 0 when total items is 0', () => {
mockGridOption.pagination.pageSize = 25;
mockGridOption.pagination.pageNumber = 2;
mockGridOption.pagination.totalItems = 0;

service.init(gridStub, dataviewStub, mockGridOption.pagination, mockGridOption.backendServiceApi);
service.recalculateFromToIndexes();

expect(service.pager.from).toBe(1);
expect(service.pager.to).toBe(1);
expect(service.pager.from).toBe(0);
expect(service.pager.to).toBe(0);
});

it('should recalculate the From/To within range', () => {
Expand Down Expand Up @@ -655,6 +655,22 @@ describe('PaginationService', () => {
done();
});
});

it('should call "processOnItemAddedOrRemoved" and expect the (To) to equal the total items when it is higher than the total pageSize count', (done) => {
mockGridOption.pagination.pageNumber = 4;
mockGridOption.pagination.totalItems = 99;
const mockItems = { name: 'John' };

service.init(gridStub, dataviewStub, mockGridOption.pagination, mockGridOption.backendServiceApi);
gridServiceStub.onItemAdded.next(mockItems);
service.changeItemPerPage(100);

setTimeout(() => {
expect(service.pager.from).toBe(1);
expect(service.pager.to).toBe(100);
done();
});
});
});

describe('with Local Grid', () => {
Expand Down
24 changes: 13 additions & 11 deletions src/app/modules/angular-slickgrid/services/pagination.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ export class PaginationService {
get pager(): Pager {
return {
from: this._dataFrom,
to: this._dataTo || 1,
to: this._dataTo,
itemsPerPage: this._itemsPerPage,
pageCount: this._pageCount || 1,
pageNumber: this._pageNumber || 1,
pageCount: this._pageCount,
pageNumber: this._pageNumber,
availablePageSizes: this._availablePageSizes,
totalItems: this._totalItems,
};
Expand Down Expand Up @@ -83,6 +83,7 @@ export class PaginationService {
this.dataView.onPagingInfoChanged.subscribe((e, pagingInfo) => {
if (this._totalItems !== pagingInfo.totalRows) {
this._totalItems = pagingInfo.totalRows;
this._paginationOptions.totalItems = this._totalItems;
this.refreshPagination(false, false);
}
});
Expand Down Expand Up @@ -233,12 +234,6 @@ export class PaginationService {
return new Promise((resolve, reject) => {
this.recalculateFromToIndexes();

if (this._dataTo > this._totalItems) {
this._dataTo = this._totalItems;
} else if (this._totalItems < this._itemsPerPage) {
this._dataTo = this._totalItems;
}

if (this._isLocalGrid && this.dataView) {
this.dataView.setPagingOptions({ pageSize: this._itemsPerPage, pageNum: (pageNumber - 1) }); // dataView page starts at 0 instead of 1
this.onPaginationChanged.next(this.pager);
Expand Down Expand Up @@ -284,16 +279,23 @@ export class PaginationService {

recalculateFromToIndexes() {
if (this._totalItems === 0) {
this._dataFrom = 1;
this._dataFrom = 0;
this._dataTo = 1;
this._pageNumber = 1;
this._pageNumber = 0;
} else {
this._dataFrom = this._pageNumber > 1 ? ((this._pageNumber * this._itemsPerPage) - this._itemsPerPage + 1) : 1;
this._dataTo = (this._totalItems < this._itemsPerPage) ? this._totalItems : (this._pageNumber * this._itemsPerPage);
if (this._dataTo > this._totalItems) {
this._dataTo = this._totalItems;
}
}

// do a final check on the From/To and make sure they are not over or below min/max acceptable values
if (this._dataTo > this._totalItems) {
this._dataTo = this._totalItems;
} else if (this._totalItems < this._itemsPerPage) {
this._dataTo = this._totalItems;
}
}

/** Reset the Pagination to first page and recalculate necessary numbers */
Expand Down

0 comments on commit 7409832

Please sign in to comment.