Skip to content

Commit df92594

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 fc43513 commit df92594

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

src/lib/paginator/paginator.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,31 @@ describe('MatPaginator', () => {
361361
.toBeNull('Expected select to be removed.');
362362
});
363363

364+
it('should emit the `page` event when the pageIndex is changed', () => {
365+
paginator.pageIndex = 2;
366+
367+
expect(component.pageEvent).toHaveBeenCalledWith(jasmine.objectContaining({
368+
previousPageIndex: 0,
369+
pageIndex: 2
370+
}));
371+
});
372+
373+
it('should emit the `page` event when the length is changed', () => {
374+
paginator.length = 1337;
375+
376+
expect(component.pageEvent).toHaveBeenCalledWith(jasmine.objectContaining({
377+
length: 1337
378+
}));
379+
});
380+
381+
it('should emit the `page` event when the pageSize is changed', () => {
382+
paginator.pageSize = 27;
383+
384+
expect(component.pageEvent).toHaveBeenCalledWith(jasmine.objectContaining({
385+
pageSize: 27
386+
}));
387+
});
388+
364389
});
365390

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

src/lib/paginator/paginator.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,26 +76,42 @@ export class MatPaginator extends _MatPaginatorBase implements OnInit, OnDestroy
7676
@Input()
7777
get pageIndex(): number { return this._pageIndex; }
7878
set pageIndex(value: number) {
79-
this._pageIndex = Math.max(coerceNumberProperty(value), 0);
80-
this._changeDetectorRef.markForCheck();
79+
const newPageIndex = Math.max(coerceNumberProperty(value), 0);
80+
const oldPageIndex = this._pageIndex;
81+
82+
if (newPageIndex !== oldPageIndex) {
83+
this._pageIndex = newPageIndex;
84+
this._emitPageEvent(oldPageIndex);
85+
this._changeDetectorRef.markForCheck();
86+
}
8187
}
8288
_pageIndex: number = 0;
8389

8490
/** The length of the total number of items that are being paginated. Defaulted to 0. */
8591
@Input()
8692
get length(): number { return this._length; }
8793
set length(value: number) {
88-
this._length = coerceNumberProperty(value);
89-
this._changeDetectorRef.markForCheck();
94+
const newLength = coerceNumberProperty(value);
95+
96+
if (newLength !== this._length) {
97+
this._length = newLength;
98+
this._emitPageEvent(this.pageIndex);
99+
this._changeDetectorRef.markForCheck();
100+
}
90101
}
91102
_length: number = 0;
92103

93104
/** Number of items to display on a page. By default set to 50. */
94105
@Input()
95106
get pageSize(): number { return this._pageSize; }
96107
set pageSize(value: number) {
97-
this._pageSize = Math.max(coerceNumberProperty(value), 0);
98-
this._updateDisplayedPageSizeOptions();
108+
const newPageSize = Math.max(coerceNumberProperty(value), 0);
109+
110+
if (newPageSize !== this._pageSize) {
111+
this._pageSize = newPageSize;
112+
this._updateDisplayedPageSizeOptions();
113+
this._emitPageEvent(this.pageIndex);
114+
}
99115
}
100116
private _pageSize: number;
101117

0 commit comments

Comments
 (0)