Skip to content
Open
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
25 changes: 25 additions & 0 deletions src/material-experimental/mdc-paginator/paginator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,31 @@ describe('MDC-based MatPaginator', () => {
const hostElement = fixture.nativeElement.querySelector('mat-paginator');
expect(hostElement.getAttribute('role')).toBe('group');
});

it('should update the page index when switching to a smaller length', fakeAsync(() => {
const fixture = createComponent(MatPaginatorApp);
const instance = fixture.componentInstance;
instance.length = 50;
instance.pageSize = 10;
instance.pageIndex = 4;
fixture.detectChanges();

expect(instance.paginator.pageIndex).toBe(4);

instance.pageEvent.calls.reset();

instance.length = 10;
fixture.detectChanges();
tick();

expect(instance.paginator.pageIndex).toBe(0);
expect(instance.pageEvent).toHaveBeenCalledWith(
jasmine.objectContaining({
previousPageIndex: 4,
pageIndex: 0,
}),
);
}));
});

function getPreviousButton(fixture: ComponentFixture<any>) {
Expand Down
25 changes: 25 additions & 0 deletions src/material/paginator/paginator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,31 @@ describe('MatPaginator', () => {
const hostElement = fixture.nativeElement.querySelector('mat-paginator');
expect(hostElement.getAttribute('role')).toBe('group');
});

it('should update the page index when switching to a smaller length', fakeAsync(() => {
const fixture = createComponent(MatPaginatorApp);
const instance = fixture.componentInstance;
instance.length = 50;
instance.pageSize = 10;
instance.pageIndex = 4;
fixture.detectChanges();

expect(instance.paginator.pageIndex).toBe(4);

instance.pageEvent.calls.reset();

instance.length = 10;
fixture.detectChanges();
tick();

expect(instance.paginator.pageIndex).toBe(0);
expect(instance.pageEvent).toHaveBeenCalledWith(
jasmine.objectContaining({
previousPageIndex: 4,
pageIndex: 0,
}),
);
}));
});

function getPreviousButton(fixture: ComponentFixture<any>) {
Expand Down
12 changes: 12 additions & 0 deletions src/material/paginator/paginator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ export abstract class _MatPaginatorBase<
}
set length(value: number) {
this._length = coerceNumberProperty(value);

const maxPageIndex = Math.max(this.getNumberOfPages() - 1, 0);
const currentPageIndex = this._pageIndex;

if (currentPageIndex > maxPageIndex && this.initialized) {
// Needs to happen on the next tick, in order to avoid "changed after checked" errors.
Promise.resolve().then(() => {
this._pageIndex = maxPageIndex;
this._emitPageEvent(currentPageIndex);
});
}

this._changeDetectorRef.markForCheck();
}
private _length = 0;
Expand Down