Skip to content

Commit 016ba5c

Browse files
andrewseguinmmalerba
authored andcommitted
fix(paginator): getNumberOfPages off by one (#10724)
Based off of #10720 (adding the demo page) Changes the paginator's `getNumberOfPages` such that it returns the actual number of pages that are on the paginator rather than the max page index. This better matches the method name and description. E.g. right now if you have one page, the function returns `0` rather than `1`, which is unexpected. Fixes #10699
1 parent 3bc52df commit 016ba5c

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

src/demo-app/paginator/paginator-demo.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ <h2>No inputs</h2>
2222
<mat-slide-toggle [(ngModel)]="showFirstLastButtons">Show first/last buttons</mat-slide-toggle>
2323
</div>
2424

25-
<mat-paginator (page)="handlePageEvent($event)"
25+
<mat-paginator #paginator
26+
(page)="handlePageEvent($event)"
2627
[length]="length"
2728
[pageSize]="pageSize"
2829
[showFirstLastButtons]="showFirstLastButtons"
@@ -32,4 +33,5 @@ <h2>No inputs</h2>
3233
</mat-paginator>
3334

3435
<div> Output event: {{pageEvent | json}} </div>
36+
<div> getNumberOfPages: {{paginator.getNumberOfPages()}} </div>
3537
</mat-card>

src/lib/paginator/paginator.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,23 @@ describe('MatPaginator', () => {
338338
}));
339339
});
340340

341+
it('should keep track of the right number of pages', () => {
342+
component.pageSize = 10;
343+
component.length = 100;
344+
fixture.detectChanges();
345+
expect(paginator.getNumberOfPages()).toBe(10);
346+
347+
component.pageSize = 10;
348+
component.length = 0;
349+
fixture.detectChanges();
350+
expect(paginator.getNumberOfPages()).toBe(0);
351+
352+
component.pageSize = 10;
353+
component.length = 10;
354+
fixture.detectChanges();
355+
expect(paginator.getNumberOfPages()).toBe(1);
356+
});
357+
341358
it('should show a select only if there are multiple options', () => {
342359
expect(paginator._displayedPageSizeOptions).toEqual([5, 10, 25, 100]);
343360
expect(fixture.nativeElement.querySelector('.mat-select')).not.toBeNull();

src/lib/paginator/paginator.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export class MatPaginator extends _MatPaginatorBase implements OnInit, OnDestroy
190190
if (!this.hasNextPage()) { return; }
191191

192192
const previousPageIndex = this.pageIndex;
193-
this.pageIndex = this.getNumberOfPages();
193+
this.pageIndex = this.getNumberOfPages() - 1;
194194
this._emitPageEvent(previousPageIndex);
195195
}
196196

@@ -201,13 +201,17 @@ export class MatPaginator extends _MatPaginatorBase implements OnInit, OnDestroy
201201

202202
/** Whether there is a next page. */
203203
hasNextPage(): boolean {
204-
const numberOfPages = this.getNumberOfPages();
205-
return this.pageIndex < numberOfPages && this.pageSize != 0;
204+
const maxPageIndex = this.getNumberOfPages() - 1;
205+
return this.pageIndex < maxPageIndex && this.pageSize != 0;
206206
}
207207

208208
/** Calculate the number of pages */
209209
getNumberOfPages(): number {
210-
return Math.ceil(this.length / this.pageSize) - 1;
210+
if (!this.pageSize) {
211+
return 0;
212+
}
213+
214+
return Math.ceil(this.length / this.pageSize);
211215
}
212216

213217

0 commit comments

Comments
 (0)