【问题标题】:Set page selection to the last page of PrimeNG p-table将页面选择设置为 PrimeNG p-table 的最后一页
【发布时间】:2019-01-18 10:14:19
【问题描述】:

我需要将我的p-table 的选定页面作为最后一页。

我能够得到我的表格对象:

@ViewChild('myTable') myTable: DataTable;

ngOnInit() {

    this.subService.subsUpdated.subscribe(result => {
      this.fetchSubcontracts();
    });

    this.appForm.subAdded.subscribe(result => {
      this.added = 4;
      this.fetchSubs();
    });

  }

我尝试将p-table 上的[first][paginatorPosition] 属性绑定到一个属性,但没有成功。

<p-table #myTable
           [value]="subs"
           sortMode="multiple"
           selectionMode="single"
           (onRowSelect)="onRowSelect($event)"
           (onRowUnselect)="onRowUnselect($event)"
           [(selection)]="selectedSub"
           [columns]="columns"
           dataKey="id"
           [paginator]="subs.length > 0"
           [rows]="5"
           [first]="added"
           >

【问题讨论】:

  • 你能创建一个 StackBlitz 或分享更多代码吗?
  • @Antikhippe 我至少在我的表格配置信息中添加了 html 以及我尝试设置页面的位置。我也不知道如何获取表格的最后一页。
  • 你现在有多少元素?
  • 我下面的回答解决了你的问题吗?任何反馈将不胜感激

标签: angular primeng primeng-turbotable


【解决方案1】:

这对我有用:

@ViewChild('dt', { static: false }) table: Table;

this.table.first = Math.floor(this.table.totalRecords / this.table.rows) * this.table.rows;
this.table.firstChange.emit(this.table.first);
this.table.onLazyLoad.emit(this.table.createLazyLoadMetadata());

【讨论】:

  • 非常感谢!这有助于我以编程方式更改 p-table 页面:)
【解决方案2】:

first 属性是要显示的第一个的索引,而不是要显示的第一页的索引。

在您的表格属性中,您已设置每页 5 行,因此如果 added 等于 4,您将始终停留在包含第 4 行的第一页。

所以添加应该是Math.floor(this.subs.length/5)

【讨论】:

    【解决方案3】:

    尝试使用onPageChange()函数:

    this.appForm.subAdded.subscribe(result => {
      // number of pages in the table
      const pageCount = Math.ceil(this.myTable.totalRecords / this.myTable.rows);
      // last page
      const page = pageCount - 1;
      this.myTable.onPageChange({
        pageCount,
        page,
        first: page * this.myTable.rows,
        rows: this.myTable.rows
      });
      this.fetchSubs();
    });
    

    而不是

    this.myTable.totalRecords
    

    你也可以使用

    this.subs.length
    

    您还可以从 HTML 中删除 [first]="added"

    【讨论】:

      猜你喜欢
      • 2019-06-22
      • 1970-01-01
      • 2019-01-23
      • 2022-01-23
      • 2016-12-11
      • 2018-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多