Skip to content

Commit c8b7edb

Browse files
committed
fix(paginator): not emitting page event when pageIndex, pageSize or length changes
Currently the paginator won't fire the `page` event if it's `pageIndex`, `length` or `pageSize` change, which means that the associated table won't be able to react. These changes emit the event if the values change. Fixes #12583.
1 parent a58c725 commit c8b7edb

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

src/material/paginator/paginator.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,34 @@ describe('MatPaginator', () => {
464464
expect(paginator.showFirstLastButtons).toBe(true);
465465
});
466466

467+
it('should emit the `page` event when the pageIndex is changed', () => {
468+
const fixture = createComponent(MatPaginatorApp);
469+
fixture.componentInstance.paginator.pageIndex = 2;
470+
471+
expect(fixture.componentInstance.pageEvent).toHaveBeenCalledWith(jasmine.objectContaining({
472+
previousPageIndex: 0,
473+
pageIndex: 2
474+
}));
475+
});
476+
477+
it('should emit the `page` event when the length is changed', () => {
478+
const fixture = createComponent(MatPaginatorApp);
479+
fixture.componentInstance.paginator.length = 1337;
480+
481+
expect(fixture.componentInstance.pageEvent).toHaveBeenCalledWith(jasmine.objectContaining({
482+
length: 1337
483+
}));
484+
});
485+
486+
it('should emit the `page` event when the pageSize is changed', () => {
487+
const fixture = createComponent(MatPaginatorApp);
488+
fixture.componentInstance.paginator.pageSize = 27;
489+
490+
expect(fixture.componentInstance.pageEvent).toHaveBeenCalledWith(jasmine.objectContaining({
491+
pageSize: 27
492+
}));
493+
});
494+
467495
});
468496

469497
function getPreviousButton(fixture: ComponentFixture<any>) {

src/material/paginator/paginator.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,26 +117,42 @@ export class MatPaginator extends _MatPaginatorBase implements OnInit, OnDestroy
117117
@Input()
118118
get pageIndex(): number { return this._pageIndex; }
119119
set pageIndex(value: number) {
120-
this._pageIndex = Math.max(coerceNumberProperty(value), 0);
121-
this._changeDetectorRef.markForCheck();
120+
const newPageIndex = Math.max(coerceNumberProperty(value), 0);
121+
const oldPageIndex = this._pageIndex;
122+
123+
if (newPageIndex !== oldPageIndex) {
124+
this._pageIndex = newPageIndex;
125+
this._emitPageEvent(oldPageIndex);
126+
this._changeDetectorRef.markForCheck();
127+
}
122128
}
123129
private _pageIndex = 0;
124130

125131
/** The length of the total number of items that are being paginated. Defaulted to 0. */
126132
@Input()
127133
get length(): number { return this._length; }
128134
set length(value: number) {
129-
this._length = coerceNumberProperty(value);
130-
this._changeDetectorRef.markForCheck();
135+
const newLength = coerceNumberProperty(value);
136+
137+
if (newLength !== this._length) {
138+
this._length = newLength;
139+
this._emitPageEvent(this.pageIndex);
140+
this._changeDetectorRef.markForCheck();
141+
}
131142
}
132143
private _length = 0;
133144

134145
/** Number of items to display on a page. By default set to 50. */
135146
@Input()
136147
get pageSize(): number { return this._pageSize; }
137148
set pageSize(value: number) {
138-
this._pageSize = Math.max(coerceNumberProperty(value), 0);
139-
this._updateDisplayedPageSizeOptions();
149+
const newPageSize = Math.max(coerceNumberProperty(value), 0);
150+
151+
if (newPageSize !== this._pageSize) {
152+
this._pageSize = newPageSize;
153+
this._updateDisplayedPageSizeOptions();
154+
this._emitPageEvent(this.pageIndex);
155+
}
140156
}
141157
private _pageSize: number;
142158

0 commit comments

Comments
 (0)