简体   繁体   English

PrimeNG:如何以编程方式更改分页器中的选定页面?

[英]PrimeNG: How to programmatically change the selected page in paginator?

I built a filtered datatable with a paginator, using PrimeNG 14 along with Angular 14.我使用 PrimeNG 14 和 Angular 14 构建了一个带分页器的过滤数据表。

I rely on server-side filtering , therefore every filter-change triggers an API-request that returns fresh table-data.我依赖服务器端过滤,因此每次过滤器更改都会触发一个返回新表数据的 API 请求。 Whenever a filter changes, the user is shown the matches of the first page and the paginator is supposed to jump back to "1".每当过滤器发生变化时,都会向用户显示第一页的匹配项,并且分页器应该跳回到“1”。 Since the latter doesn't seem to work out of the box, I tried to set the desired paginator state programmatically.由于后者似乎无法开箱即用,因此我尝试以编程方式设置所需的分页器 state。 Sadly, I wasn't successful.可悲的是,我没有成功。

Please have a look at my stackblitz example or at the following code:请查看我的stackblitz 示例或以下代码:

My Component.ts:我的 Component.ts:

@ViewChild('paginator', { static: true })
paginator?: Paginator;

ngAfterViewInit(): void {
  setTimeout(() => {
    this.paginator.changePage(3);
    console.log('Currently selected page:', this.paginator.getPage());
  });
}

The console output reads Currently selected page: 3 , yet on the paginator 1 remains selected.控制台 output 读取Currently selected page: 3 ,但在分页器1上仍保持选中状态。

My Component.html:我的 Component.html:

<p-paginator
  #paginator
  [rows]="10"
  [totalRecords]="120"
  [rowsPerPageOptions]="[10, 20, 30]">
</p-paginator>

Have you got any suggestion on how to resolve this issue?您对如何解决此问题有任何建议吗?

It seems to be a primeng issue, I would recommend you opening a issue on GH这似乎是一个 primeng 问题,我建议你在 GH 上打开一个问题

Here is the workaround I did这是我所做的解决方法

public first = 0;
/*
...
*/
 private _changePage(page: number) {
    if(page < this.paginator.getPageCount())
      this.first = (page-1)*this.paginator.rows
  }


// add this to your paginator [first]="first"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM