Skip to content

fix(material/paginator): emit events when page index is updated #30890

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions src/material/paginator/paginator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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);
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/material/table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Loading