Skip to content

Commit

Permalink
feat(Paginator): show "out of" total to simple paginator if total is …
Browse files Browse the repository at this point in the history
…available

We want to show "out of ___" on the simple paginator page label if the total is available. If it is
not, show the shortened version instead

BREAKING CHANGE:

ISSUES CLOSED: #2047
  • Loading branch information
deanterm authored and benjamincharity committed Mar 5, 2020

Verified

This commit was signed with the committer’s verified signature.
fabpot Fabien Potencier
1 parent 5eae26e commit dd6c648
Showing 2 changed files with 32 additions and 4 deletions.
8 changes: 8 additions & 0 deletions projects/library/paginator/src/paginator.component.spec.ts
Original file line number Diff line number Diff line change
@@ -351,7 +351,15 @@ describe(`TsPaginatorComponent`, function() {
fixture.detectChanges();
});

test(`should output the correct, current page message`, fakeAsync(() => {
tick(1000);
fixture.detectChanges();
const titleEl = fixture.debugElement.query(By.css('.c-paginator__current-page')).nativeElement as HTMLElement;
expect(titleEl.textContent).toContain('1 - 10 of 100');
}));

test(`should output the correct, shortened, current page message`, fakeAsync(() => {
fixture.componentInstance.totalRecords = 0;
tick(1000);
fixture.detectChanges();
const titleEl = fixture.debugElement.query(By.css('.c-paginator__current-page')).nativeElement as HTMLElement;
28 changes: 24 additions & 4 deletions projects/library/paginator/src/paginator.component.ts
Original file line number Diff line number Diff line change
@@ -489,16 +489,36 @@ export class TsPaginatorComponent implements OnChanges, AfterViewInit {

// This may be the case if there are no records
if (!foundPage || !foundPage.name) {
foundPage = { name: '0' };
return this.createDefaultPageLabel(currentPage, totalRecords);
}

// '1 - 10 of 243'
if (this.isSimpleMode) {
return `${foundPage.name}`;
}
return `${foundPage.name} of ${totalRecords}`;
}

/**
* Create a default label based on the records per page and total records
*
* @param currentPage - The current page
* @param totalRecords - The number of total records
* @return The string to use as the current page label
*/
private createDefaultPageLabel(
currentPage: number,
totalRecords: number,
): string {
const start = this.isZeroBased
? (currentPage * this.recordsPerPage)
: (currentPage - 1) * this.recordsPerPage;
const end = start + this.recordsPerPage;
// '1 - 10'
if (this.isSimpleMode && !totalRecords) {
return `${start + 1} - ${end}`;
}
// '1 - 10 of 243'
return `${start + 1} - ${end} of ${totalRecords}`;
}


/**
* Create an array containing objects that represent each available page of records

0 comments on commit dd6c648

Please sign in to comment.