Search code examples
angularprimeng

PrimeNG Paginator set Page


I'm using the primeng paginator component inside an Angular 9 Project to page a huge list of profiles. If a user is on page 3, opens a profile and returns to search, the current page is 1, but it should be 3. I have saved the page inside a service, so the right data is shown, but the paginator is showing the wrong page. I could not find a way to set the current page of the paginator. Using the [first] attribute is not working.


Solution

  • You need to set the page manually. You can use Angular @ViewChild decorator to get access to PrimeNg Paginator component.

    Give an id to that paginator in template:

    <p-paginator #paginator ...></p-paginator>
    

    And use it in component:

    import { Paginator } from 'primeng/paginator';
    
    
    @ViewChild('paginator', { static: true }) paginator: Paginator
    
    private updateCurrentPage(currentPage: number): void {
      setTimeout(() => this.paginator.changePage(currentPage));
    }