Skip to content

Commit 19e617f

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 3ca3e39 commit 19e617f

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
@@ -414,6 +414,31 @@ describe('MatPaginator', () => {
414414
expect(getLastButton(fixture).hasAttribute('disabled')).toBe(true);
415415
});
416416

417+
it('should emit the `page` event when the pageIndex is changed', () => {
418+
paginator.pageIndex = 2;
419+
420+
expect(component.pageEvent).toHaveBeenCalledWith(jasmine.objectContaining({
421+
previousPageIndex: 0,
422+
pageIndex: 2
423+
}));
424+
});
425+
426+
it('should emit the `page` event when the length is changed', () => {
427+
paginator.length = 1337;
428+
429+
expect(component.pageEvent).toHaveBeenCalledWith(jasmine.objectContaining({
430+
length: 1337
431+
}));
432+
});
433+
434+
it('should emit the `page` event when the pageSize is changed', () => {
435+
paginator.pageSize = 27;
436+
437+
expect(component.pageEvent).toHaveBeenCalledWith(jasmine.objectContaining({
438+
pageSize: 27
439+
}));
440+
});
441+
417442
});
418443

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

src/lib/paginator/paginator.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,26 +90,42 @@ export class MatPaginator extends _MatPaginatorBase implements OnInit, OnDestroy
9090
@Input()
9191
get pageIndex(): number { return this._pageIndex; }
9292
set pageIndex(value: number) {
93-
this._pageIndex = Math.max(coerceNumberProperty(value), 0);
94-
this._changeDetectorRef.markForCheck();
93+
const newPageIndex = Math.max(coerceNumberProperty(value), 0);
94+
const oldPageIndex = this._pageIndex;
95+
96+
if (newPageIndex !== oldPageIndex) {
97+
this._pageIndex = newPageIndex;
98+
this._emitPageEvent(oldPageIndex);
99+
this._changeDetectorRef.markForCheck();
100+
}
95101
}
96102
_pageIndex: number = 0;
97103

98104
/** The length of the total number of items that are being paginated. Defaulted to 0. */
99105
@Input()
100106
get length(): number { return this._length; }
101107
set length(value: number) {
102-
this._length = coerceNumberProperty(value);
103-
this._changeDetectorRef.markForCheck();
108+
const newLength = coerceNumberProperty(value);
109+
110+
if (newLength !== this._length) {
111+
this._length = newLength;
112+
this._emitPageEvent(this.pageIndex);
113+
this._changeDetectorRef.markForCheck();
114+
}
104115
}
105116
_length: number = 0;
106117

107118
/** Number of items to display on a page. By default set to 50. */
108119
@Input()
109120
get pageSize(): number { return this._pageSize; }
110121
set pageSize(value: number) {
111-
this._pageSize = Math.max(coerceNumberProperty(value), 0);
112-
this._updateDisplayedPageSizeOptions();
122+
const newPageSize = Math.max(coerceNumberProperty(value), 0);
123+
124+
if (newPageSize !== this._pageSize) {
125+
this._pageSize = newPageSize;
126+
this._updateDisplayedPageSizeOptions();
127+
this._emitPageEvent(this.pageIndex);
128+
}
113129
}
114130
private _pageSize: number;
115131

0 commit comments

Comments
 (0)