Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I set pageIndex of paginator and it can update itself? #6220

Closed
mrdulin opened this issue Aug 2, 2017 · 14 comments
Closed

How can I set pageIndex of paginator and it can update itself? #6220

mrdulin opened this issue Aug 2, 2017 · 14 comments

Comments

@mrdulin
Copy link

mrdulin commented Aug 2, 2017

Bug, feature request, or proposal:

proposal

What is the expected behavior?

For example. When the paginator display 11-20 of 400 , I want to set the pageIndex to initial state. And after that, I want paginator display 1-10 of 400.

What is the current behavior?

First page is 0 and current page is 1, the paginator view display 11-20 of 400.

I set the pageIndex to initial state like this:

this.paginator.pageIndex = 0;

But the view of paginator not update. Still display 11-20 of 400.

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

"@angular/core": "^4.3.1",
"@angular/material": "^2.0.0-beta.8",

@willshowell
Copy link
Contributor

See #5904 and #5822

@mrdulin
Copy link
Author

mrdulin commented Aug 2, 2017

@willshowell

 public onSubmit(): void {
    const firstPage: number = this.pageIndex + 1;
    this.paginator.pageIndex = 0;
    this.diseaseDataBase.getDiseasesByPage(this.keyword, firstPage);
  }

first submit, the keyword is java, and go to page 1, paginator display: 11-20 of 400.

But when I submit again with the same keyword java, the paginator still display: 11-20 of 400, even if I set paginator.pageIndex = 0.

@andrewseguin
Copy link
Contributor

Fixed by #5822

@mrdulin
Copy link
Author

mrdulin commented Aug 3, 2017

@andrewseguin I don't think it has been fixed. If you can test it to see.

I took a look for the source code of paginator.

/** The zero-based page index of the displayed list of items. Defaulted to 0. */
  @Input()
  get pageIndex(): number { return this._pageIndex; }
  set pageIndex(pageIndex: number) {
    this._pageIndex = pageIndex;
    this._changeDetectorRef.markForCheck();
  }
  _pageIndex: number = 0;

I found this._changeDetectorRef.markForCheck();. I think it's should be update the view when I set the paginator.pageIndex again. But it's not.

@willshowell
Copy link
Contributor

@mrdulin the change made in #5822 is not part of beta-8. It will be available in the next release!

@mrdulin
Copy link
Author

mrdulin commented Aug 3, 2017

@willshowell oh. That's the reason. when is the next release ?

@TomBak5
Copy link

TomBak5 commented Oct 9, 2017

hy. I have same problem too. Sorry guys, but this.mdPaginator.pageIndex = 0; is not working for me when I do it in some custom function. Can anyone explain me how to go to first page without reloading the hole page? I tried it with angular/material 2.0.0-beta.(8,9,10) with respective CDK versions and it doesn't work as I expected.

@willshowell
Copy link
Contributor

@srokatonie
Copy link

Full solution:

You need to declare paginator dom element as variable using #paginator

<!-- my.component.html -->
<mat-paginator #paginator></mat-paginator>

Then set every time you need to set pageIndex:

//my.component.ts
this.paginator._pageIndex = 0;

@dfhincapiem
Copy link

dfhincapiem commented Jan 9, 2019

I do have done everything that you guys have been talking about. But when I set my component like this

<mat-paginator #paginator>
<button (click)="test()">TEST

And in the component

test() {
this.paginator.pageIndex = 1;
}

And when the page loads appears something like this:

Items per page:
10
1 - 10 of 26

And when I click the button, this appears:

tems per page:
10
11 - 20 of 26

It seems that the pageIndex was updated but the data of the table is not refreshed, please any help would be appreciated

@thw0rted
Copy link

@dfhincapiem see #8417, this is a known issue from over a year ago and I'm kind of amazed it hasn't been fixed yet. There is a hack / workaround described in that issue.

@andzejsw
Copy link

I made load more instead of paginator for my project needs.

private loadMore() { this.collection.paginator.pageSize += 1; this.collection.paginator._emitPageEvent(0) }

Basically _emitPageEvent(YourPageIndex) reloads page!

@maskalek
Copy link

MatPaginator contains method to move to the first page.

this.paginator.firstPage()

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants