【问题标题】:PrimeNG DataTable server-side pagingPrimeNG DataTable 服务器端分页
【发布时间】:2017-08-09 14:57:50
【问题描述】:

我目前正在开发一个 Angular 4 项目并使用 PrimeNG 数据表。到目前为止,这个框架看起来很整洁,但我想让我的分页服务器端。这样我一次只加载 10,20,.. 记录,而不是一次加载所有 1000+..

以前有没有人这样做过,或者有人知道解决办法吗?

PS:如果您没有解决方案但知道支持此功能的框架,请告诉我!

【问题讨论】:

    标签: angular primeng primeng-datatable


    【解决方案1】:

    看起来 Lazy 是我们正在寻找的东西……正在寻找 :) https://www.primefaces.org/primeng/#/table/lazy

    【讨论】:

    • 答案中的链接已过时。这是更新的链接:primefaces.org/primeng/#/table/lazy
    • 在将数据加载到表之前,我们真的必须知道总记录数吗?
    • 是的,需要申请分页
    【解决方案2】:

    借助延迟加载,我们可以对数据表实现服务器端的分页、过滤和排序。

    代码如下:

    listing.html

    <div class="table-responsive">
      <p-dataTable
        sortField="FileNo"
        [sortOrder]="1"
        [value]="paitientListing"
        [lazy]="true"
        [rows]="10"
        [paginator]="true"
        [rowsPerPageOptions]="[10,20]"
        [totalRecords]="totalRecords"
        (onLazyLoad)="loadPatientListing($event)"
      >
        <p-column
          field="PatientID"
          header="File No"
          [sortable]="true"
          [style]="{'width':'94px'}"
        ></p-column>
        <p-column
          field="LastName"
          [sortable]="true"
          [style]="{'width':'121px'}"
          header="Last Name"
        ></p-column>
        <p-column
          field="FirstName"
          [sortable]="true"
          [style]="{'width':'122px'}"
          header="First Name"
        ></p-column>
        <p-column styleClass="col-button" [style]="{'width':'90px'}">
          <ng-template pTemplate="header"> Action </ng-template>
          <ng-template let-paitientListing="rowData" pTemplate="body">
            <button
              type="button"
              pButton
              (click)="editPatient(paitientListing.PatientID)"
              icon="fa-pencil-square-o"
            ></button>
            <button
              type="button"
              pButton
              (click)="deletePatient(paitientListing.PatientID)"
              icon="fa-trash-o"
              class="ui-button-danger"
            ></button>
          </ng-template>
        </p-column>
      </p-dataTable>
    </div>
    

    listing.Component.ts

    loadPatientListing(event) {
      this.patientFilterModel.PageSize = event.rows;
      this.patientFilterModel.RowNumber = event.first;
      this.patientFilterModel.OrderColumn = event.sortField;
    
      if (event.sortOrder == 1) {
        this.patientFilterModel.OrderBy = 'asc';
      } else {
        this.patientFilterModel.OrderBy = 'desc';
      }
      this.patientService.GetPatientListing(this.patientFilterModel).subscribe(
        (data) => {
          this.patientModel = data;
          this.paitientListing = this.patientModel._ListPatientListing;
          this.totalRecords = data.TotalRecords;
        },
        (error) => {
          this.loading = false;
        },
      );
    }
    

    【讨论】:

    • 您能否为您的代码提供一些解释。例如什么是 'patientFilterModel' 和 _ListPatientListing 是什么?
    • 考虑到 已被 取代,您能否更新答案?谢谢。
    【解决方案3】:

    仅供参考,p-dataTable 在版本 6 中已弃用。PrimeFaces 建议您使用 TurboTable。我只需要完成转换。您将需要添加 [totalRecords]='totalRecords' [lazy]='true' (onLazyLoad)='loadPatientLazy($event)' [loading]='loading'

    <p-table id='patients-grid' [value]='patients' [totalRecords]='totalRecords'
            expandableRows='true' [responsive]='true' dataKey=''
            [rows]='5' [paginator]='true' [rowsPerPageOptions]='[5,10,50]'>
        <ng-template pTemplate='header'>
            <tr>
                <th style='width: 40px;'></th>
                <th style='width: 40px;'>
                    <button (click)='addItemClicked( )' pButton type='button' icon='fa fa-plus' class='ui-button-primary'></button>
                </th>
                <th style='width: 80px;' [pSortableColumn]='"PatientID"'>
                    Patient I D
                    <p-sortIcon [field]='PatientID'></p-sortIcon>
                </th>
                <th style='width: 250px;' [pSortableColumn]='"LastName"'>
                    Last Name
                    <p-sortIcon [field]='LastName'></p-sortIcon>
                </th>
                <th style='width: 250px;' [pSortableColumn]='"FirstName"'>
                    First Name
                    <p-sortIcon [field]='FirstName'></p-sortIcon>
                </th>
                <th style='width: 40px;'></th>
            </tr>
        </ng-template>
        <ng-template pTemplate='body' let-rowData let-columns='columns' let-expanded='expanded'>
            <tr>
                <td>
                    <a href='#' [pRowToggler]='rowData'>
                        <i [ngClass]='expanded ? "pi pi-fw pi-chevron-circle-down" : "pi pi-pw pi-chevron-circle-right"' style='font-size: 1.25em'></i>
                    </a>
                </td>
                <td>
                    <button (click)='editItemClicked( rowData )' pButton type='button' icon='fa fa-edit' class='ui-button-primary'></button>
                </td>
                <td>{{rowData['PatientID']}}</td>
                <td>{{rowData['LastName']}}</td>
                <td>{{rowData['FirstName']}}</td>
                <td>
                    <button (click)="deleteItemClicked( patient )" pButton type="button" icon="fa fa-trash" class="ui-button-danger"></button>
                </td>
            </tr>
        </ng-template>
        <ng-template let-patient pTemplate='rowexpansion'>
            <tr><td [attr.colspan]='6'>
                <div class='ui-grid ui-grid-responsive ui-fluid'>
                    <div class='ui-grid-row ui-inputgroup'>
                        <div class='ui-grid-col-3 nsg-primary-color nsg-text-right'><label for='PatientID'>Patient I D:&nbsp;</label></div>
                        <div class='ui-grid-col-9' id='PatientID'>{{patient.PatientID}}</div>
                    </div>
                    <div class='ui-grid-row ui-inputgroup'>
                        <div class='ui-grid-col-3 nsg-primary-color nsg-text-right'><label for='LastName'>Last Name:&nbsp;</label></div>
                        <div class='ui-grid-col-9' id='LastName'>{{patient.LastName}}</div>
                    </div>
                    <div class='ui-grid-row ui-inputgroup'>
                        <div class='ui-grid-col-3 nsg-primary-color nsg-text-right'><label for='FirstName'>First Name:&nbsp;</label></div>
                        <div class='ui-grid-col-9' id='FirstName'>{{patient.FirstName}}</div>
                    </div>
                </div>
            </td><tr>
        </ng-template>
    </p-table>
    

    注意:nsg- CSS 类是我的自定义类。

    【讨论】:

    • 同时添加 dataKey='createdate'
    • 另外,请确保您使用的是 HttpClient,不推荐使用 Http。 RxJs 的第 6 版非常不同。
    【解决方案4】:

    您可以监听 PaginatoronPageChange 事件,告诉您何时需要获取 paginator.rows 的数据。

    【讨论】:

      【解决方案5】:

      首先你不需要对 p-table 组件进行分页,因此 paginator 属性会像这样为假

      <p-table
                  [value]="clientList"
                  [rows]="pageSize"
                  [paginator]="false"
                  [(selection)]="selectedClients"
                  [responsive]="true"
                  styleClass="p-datatable-lg p-datatable-responsive"
                >
      

      其次,您应该在表格下方使用这样的分页器组件

      <p-table
                  [value]="clientList"
                  [rows]="pageSize"
                  [paginator]="false"
                  [(selection)]="selectedClients"
                  [responsive]="true"
                  styleClass="p-datatable-lg p-datatable-responsive"
                >
      <!-- your content goes here !-->
      </table>
         <p-paginator
                  (onPageChange)="handlePagination($event)"
                  [rows]="pageSize"
                  [totalRecords]="totalRecords"
                ></p-paginator>  
      
       public handlePagination(paginationData): void {
          this.currentPage = paginationData.page + 1;
          this.filterProducts();
        }
      
      
      
        public filterProducts(): void {
          this.isLoading = true;
          this.subs.sink = this.productService
            .filterProducts(
              this.appId,
              this.searchedProduct,
              this.selectedLangISO2,
              this.currentPage,
              this.sortBy,
              this.sortField,
              this.pageSize,
              ...this.additionalFilters
            )
            .subscribe(
              (result) => {
      })
      

      【讨论】:

        【解决方案6】:

        对于使用旧版本的人,信息可在 https://www.primefaces.org/primeng-4.3.0/#/datatable

        loadData(event: LazyLoadEvent) {
            //event.first = First row offset
            //event.rows = Number of rows per page
            //event.sortField = Field name to sort in single sort mode
            //event.sortOrder = Sort order as number, 1 for asc and -1 for dec in single sort mode
            //multiSortMeta: An array of SortMeta objects used in multiple columns sorting. Each SortMeta has field and order properties.
            //filters: Filters object having field as key and filter value, filter matchMode as value
            //globalFilter: Value of the global filter if available
            this.cars = //do a request to a remote datasource using a service and return the cars that match the lazy load criteria
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-04-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-27
          • 2016-05-04
          • 2018-02-12
          相关资源
          最近更新 更多