简体   繁体   English

如何在primeng数据表中显示分页详细信息

[英]how to show pagination details in primeng datatable

In primeng datatable i am using filter and getting pagination.在primeng 数据表中,我正在使用过滤器并进行分页。 But want to pagination details.但是想要分页详细信息。

eg:例如:

showing 1 to 10 of 100 entries,显示 100 个条目中的 1 到 10 个,

I am using datatable from following link.我正在使用以下链接中的数据表。

https://www.primefaces.org/primeng-5.2.7/#/datatable/filter https://www.primefaces.org/primeng-5.2.7/#/datatable/filter

First set an event handler for onPage event which is triggered whenever pagination happens.首先为 onPage 事件设置一个事件处理程序,该事件在发生分页时触发。 Then set the custom pagination string on that handler.然后在该处理程序上设置自定义分页字符串。

    <p-dataTable [value]="cars" [rows]="10" [paginator]="true" [pageLinks]="3" [rowsPerPageOptions]="[5,10,20]" (onPage)="setMyPagination(event)">
        <p-header>List of Cars</p-header>
        <p-column field="vin" header="Vin"></p-column>
        <p-column field="year" header="Year"></p-column>
        <p-column field="brand" header="Brand"></p-column>
        <p-column field="color" header="Color"></p-column>
        <ng-template pTemplate="paginatorLeft">
            {{myPaginationString}}
        </ng-template>
        <ng-template pTemplate="paginatorRight">
           {{myPaginationString}}
        </ng-template>
    </p-dataTable>`  
    myPaginationString = "";

setMyPagination(event) {
    //event.first: Index of first record being displayed 
    //event.rows: Number of rows to display in new page 
    //event.page: Index of the new page 
    //event.pageCount: Total number of pages
    let startRow = event.first + 1;
    let endRow =  startRow + event.rows;
    let totalRows = this.cars.length;
    this.myPaginationString  = "showing "+startRow +" to "+ endRow +" of "+ totalRows  +" entries"
}

You could do this:你可以这样做:

<ng-template pTemplate="paginatorleft" let-state>
        Showing {{(state.page  * state.rows) + 1}} to {{state.rows * (state.page + 1)}} of {{state.totalRecords}} entries
  </ng-template>

For adding Pagination to PrimeNG Table as follows -将分页添加到 PrimeNG 表如下 -

在此处输入图片说明

  1. Create a backing object to hold the total number of records to be rendered using PrimeNG Datatable创建一个支持对象来保存要使用 PrimeNG 数据表呈现的记录总数

     this.totalRecords=this.books.length;

Next we adding the following attributes for the datatable-接下来我们为数据表添加以下属性-

  • paginator - We set the paginate property to true to enable Pagination for the datatable paginator - 我们将 paginate 属性设置为 true 以启用数据表的分页
  • rows - We define how many rows should be displayed per page.行 - 我们定义每页应显示多少行。
  • rowsPerPageOptions - Allow the user to choose the number of rows user wants per page. rowsPerPageOptions - 允许用户选择用户希望每页的行数。
  • totalRecords - The total number of records the data table will hold. totalRecords - 数据表将保存的记录总数。 We define this backing object above.我们在上面定义了这个支持对象。
  • pageLinks - Show the number of Pagination links. pageLinks - 显示分页链接的数量。

     <p-table [columns]="cols" [value]="books" [paginator]="true" [rows]="10" [rowsPerPageOptions]="[5,10,15,20]" totalRecords="totalRecords" pageLinks="3"> <ng-template pTemplate="header" let-columns> <tr> <th *ngFor="let col of columns"> {{col.header}} </th> </tr> </ng-template> <ng-template pTemplate="body" let-rowData let-columns="columns"> <tr> <td *ngFor="let col of columns"> {{rowData[col.field]}} </td> </tr> </ng-template> </p-table>

Reference - PrimeNG Tutorial - DataTable Pagination (Paginator) Example with Source Code and Video Tutorial参考 - PrimeNG 教程 - DataTable Pagination (Paginator) Example with Source Code and Video Tutorial

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

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