diff --git a/src/material/paginator/paginator.ts b/src/material/paginator/paginator.ts index 2d429a7e2d7b..e08a1a64961d 100644 --- a/src/material/paginator/paginator.ts +++ b/src/material/paginator/paginator.ts @@ -137,8 +137,7 @@ export class MatPaginator implements OnInit, OnDestroy { return this._pageIndex; } set pageIndex(value: number) { - this._pageIndex = Math.max(value || 0, 0); - this._changeDetectorRef.markForCheck(); + this._navigate(value); } private _pageIndex = 0; @@ -306,11 +305,8 @@ export class MatPaginator implements OnInit, OnDestroy { // Current page needs to be updated to reflect the new page size. Navigate to the page // containing the previous page's first item. const startIndex = this.pageIndex * this.pageSize; - const previousPageIndex = this.pageIndex; - - this.pageIndex = Math.floor(startIndex / pageSize) || 0; this.pageSize = pageSize; - this._emitPageEvent(previousPageIndex); + this.pageIndex = Math.floor(startIndex / pageSize) || 0; } /** Checks whether the buttons for going forwards should be disabled. */ @@ -361,11 +357,11 @@ export class MatPaginator implements OnInit, OnDestroy { /** Navigates to a specific page index. */ private _navigate(index: number) { - const previousIndex = this.pageIndex; - - if (index !== previousIndex) { - this.pageIndex = index; - this._emitPageEvent(previousIndex); + if (index !== this.pageIndex) { + const previousPageIndex = this._pageIndex; + this._pageIndex = Math.max(index || 0, 0); + this._changeDetectorRef.markForCheck(); + this._emitPageEvent(previousPageIndex); } } diff --git a/src/material/table/table.spec.ts b/src/material/table/table.spec.ts index 20debc1c261c..aefcaea28be4 100644 --- a/src/material/table/table.spec.ts +++ b/src/material/table/table.spec.ts @@ -566,6 +566,18 @@ describe('MatTable', () => { ['a_10', 'b_10', 'c_10'], ['Footer A', 'Footer B', 'Footer C'], ]); + + component.paginator.pageIndex = 0; + fixture.detectChanges(); + expectTableToMatchContent(tableElement, [ + ['Column A', 'Column B', 'Column C'], + ['a_1', 'b_1', 'c_1'], + ['a_2', 'b_2', 'c_2'], + ['a_3', 'b_3', 'c_3'], + ['a_4', 'b_4', 'c_4'], + ['a_5', 'b_5', 'c_5'], + ['Footer A', 'Footer B', 'Footer C'], + ]); })); it('should sort strings with numbers larger than MAX_SAFE_INTEGER correctly', () => {