Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pagination): don't hide single page number with ellipsis (#1235) #3419

Merged
merged 1 commit into from Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 55 additions & 1 deletion src/pagination/pagination.spec.ts
Expand Up @@ -440,7 +440,7 @@ describe('ngb-pagination', () => {

fixture.componentInstance.page = 4;
fixture.detectChanges();
expectPages(fixture.nativeElement, ['«', '1', '-...', '3', '+4', '5', '-...', '7', '»']);
expectPages(fixture.nativeElement, ['«', '1', '2', '3', '+4', '5', '6', '7', '»']);

fixture.componentInstance.page = 5;
fixture.detectChanges();
Expand All @@ -461,6 +461,60 @@ describe('ngb-pagination', () => {
expectPages(fixture.nativeElement, ['«', '1', '2', '3', '4', '+5', '6', '7', '»']);
});

it('should use page number instead of ellipsis when ellipsis hides a single page', () => {
const html = `<ngb-pagination [collectionSize]="120" [page]="page"
[maxSize]="5" [rotate]="true" [ellipses]="true"></ngb-pagination>`;
const fixture = createTestComponent(html);

fixture.componentInstance.page = 1;
fixture.detectChanges();
expectPages(fixture.nativeElement, ['-«', '+1', '2', '3', '4', '5', '-...', '12', '»']);

fixture.componentInstance.page = 2;
fixture.detectChanges();
expectPages(fixture.nativeElement, ['«', '1', '+2', '3', '4', '5', '-...', '12', '»']);

fixture.componentInstance.page = 3;
fixture.detectChanges();
expectPages(fixture.nativeElement, ['«', '1', '2', '+3', '4', '5', '-...', '12', '»']);

fixture.componentInstance.page = 4;
fixture.detectChanges();
expectPages(fixture.nativeElement, ['«', '1', '2', '3', '+4', '5', '6', '-...', '12', '»']);

fixture.componentInstance.page = 5;
fixture.detectChanges();
expectPages(fixture.nativeElement, ['«', '1', '2', '3', '4', '+5', '6', '7', '-...', '12', '»']);

fixture.componentInstance.page = 6;
fixture.detectChanges();
expectPages(fixture.nativeElement, ['«', '1', '-...', '4', '5', '+6', '7', '8', '-...', '12', '»']);

fixture.componentInstance.page = 7;
fixture.detectChanges();
expectPages(fixture.nativeElement, ['«', '1', '-...', '5', '6', '+7', '8', '9', '-...', '12', '»']);

fixture.componentInstance.page = 8;
fixture.detectChanges();
expectPages(fixture.nativeElement, ['«', '1', '-...', '6', '7', '+8', '9', '10', '11', '12', '»']);

fixture.componentInstance.page = 9;
fixture.detectChanges();
expectPages(fixture.nativeElement, ['«', '1', '-...', '7', '8', '+9', '10', '11', '12', '»']);

fixture.componentInstance.page = 10;
fixture.detectChanges();
expectPages(fixture.nativeElement, ['«', '1', '-...', '8', '9', '+10', '11', '12', '»']);

fixture.componentInstance.page = 11;
fixture.detectChanges();
expectPages(fixture.nativeElement, ['«', '1', '-...', '8', '9', '10', '+11', '12', '»']);

fixture.componentInstance.page = 12;
fixture.detectChanges();
expectPages(fixture.nativeElement, ['«', '1', '-...', '8', '9', '10', '11', '+12', '-»']);
});

it('should handle edge "maxSize" values', () => {
const html = '<ngb-pagination [collectionSize]="50" [maxSize]="maxSize"></ngb-pagination>';
const fixture = createTestComponent(html);
Expand Down
16 changes: 14 additions & 2 deletions src/pagination/pagination.ts
Expand Up @@ -286,14 +286,26 @@ export class NgbPagination implements OnChanges {
private _applyEllipses(start: number, end: number) {
if (this.ellipses) {
if (start > 0) {
if (start > 1) {
// The first page will always be included. If the displayed range
// starts after the third page, then add ellipsis. But if the range
// starts on the third page, then add the second page instead of
// an ellipsis, because the ellipsis would only hide a single page.
if (start > 2) {
this.pages.unshift(-1);
} else if (start === 2) {
this.pages.unshift(2);
}
this.pages.unshift(1);
}
if (end < this.pageCount) {
if (end < (this.pageCount - 1)) {
// The last page will always be included. If the displayed range
// ends before the third-last page, then add ellipsis. But if the range
// ends on third-last page, then add the second-last page instead of
// an ellipsis, because the ellipsis would only hide a single page.
if (end < (this.pageCount - 2)) {
this.pages.push(-1);
} else if (end === (this.pageCount - 2)) {
this.pages.push(this.pageCount - 1);
}
this.pages.push(this.pageCount);
}
Expand Down