Bug
Changing the page index programmatically should emit a page
event.
Assigning to .pageIndex
does not emit a page
event. There is no method to jump to a specified page and ensure that an event is emitted.
Open this plunker. Enter a (different) page number in the input at the top of the page and click "Go To". The displayed paginator offset ("11-20") changes but the table results do not.
The current behavior is wrong. The documentation for the page
Output says "Event emitted when the paginator changes the page size or page index"; the page index has changed but the event was not emitted.
Still happening as of Beta 12, at least.
I checked paginator.ts and the event is emitted in various methods that change the page index (next / previous) but not in the setting for pageIndex itself. I don't know why this design decision was made so I hesitate to suggest that it be changed, but we need some way to tell the paginator to go to a page as though the user had clicked to get to that page.
In the short term, I noticed that the private method _changePageSize
is exposed in the typings for this component, and does not check if the size being set is the current size. So I can call
this.paginator._changePageSize(this.paginator.pageSize);
to force the paginator to emit a page
event. Of course it's a pretty awful hack, but it gets the intended behavior. It'd be nice to avoid this in a future release.
I meant to leave a trackback to #6220 in the original issue -- not exactly the same thing, but related.
Came here with the same problem. I needed a way to keep paginator on the same page after a data update for a mattable. The pageSize hack from @thw0rted is still the best option.
For others using paginator with mattable, I had to set the pageIndex inside a timeout in order for paginator to detect that the pageIndex was changed:
@ViewChild(MatPaginator) paginator: MatPaginator;
public dataSource: MatTableDataSource<{ [id: string]: any }>;
...
refreshPaginator() {
if (!this.dataSource.paginator) {
this.dataSource.paginator = this.paginator;
}
let pageIndex = 0;
... // some math
setTimeout((idx) => {
this.paginator._pageIndex = idx;
this.paginator._changePageSize(this.paginator.pageSize);
}, 0, pageIndex);
}
I had to add this.paginator._changePageSize(this.paginator.pageSize); to get mine working also
It says : "Property '_pageIndex' is private and only accessible within class 'MatPaginator'"
Quite some time ago, I believe they added a public accessor for pageIndex
so you don't have to use the underscore-prefixed field anymore. You still have to use the prefix for _changePageSize
though.
I haven't looked at this in ages. @andrewseguin is there any debate about this? I'm pretty sure it's unambiguously a bug, and can be fixed by inserting a call to _emitPageEvent
in the setter for pageIndex. Do you know why this has sat open for a year and a half? Would you accept a PR?
ETA: while we're at it, can we talk about why there are two different ways to change the page size (pageSize
and _changePageSize()
), one of which emits a page event and one of which doesn't?
Hello again as we near the 2-year anniversary of this bug! #6220 just got auto-closed so I figured I should stop by here to provide some activity and avoid the same fate. Ping! 馃榿
@thw0rted Just FYI - that bot only _locks_ already closed issues, it doesn't close them. So open issues like this should be OK without a bump.
Hi all any workaround on this ?
Hello Everyone. Here is My working example.
first you need to change paginator.pageIndex
and emit event .page for a change page
this.paginator.pageIndex = this.currentPage;
this.dataSource.paginator.page.emit({
length: this.paginator.getNumberOfPages(),
pageIndex: this.currentPage,
pageSize: 10,
previousPageIndex: this.previousPage
})
AshotAleqs solution works very good - without flickering (as with setTimeout). Little improvement:
this.paginator.pageIndex = pageIndex; this.dataSource.paginator.page.emit({ length: this.paginator.getNumberOfPages(), pageIndex: pageIndex, pageSize: this.paginator.pageSize })
@b-mi ah I see, Thank you. I forgot to change my properties
will this ever be fixed? :)
What version is used where emitting the page event or using the public pageIndex
works? In 7.2, the only working solution is still the one I posted above with _pageIndex
and _changePageSize
(use // @ts-ignore
to ignore the private variable error)
Hello, @alexpearce92 I have used a 7.2 version of angular. And the solution works correctly. without any errors
What version is used where emitting the page event or using the public
pageIndex
works? In 7.2, the only working solution is still the one I posted above with_pageIndex
and_changePageSize
(use// @ts-ignore
to ignore the private variable error)
Only works on my end with the setTimeout
Most helpful comment
AshotAleqs solution works very good - without flickering (as with setTimeout). Little improvement: