【问题标题】:Ngx-boostrap pagination not working as desiredNgx-bootstrap 分页无法按预期工作
【发布时间】:2020-08-06 01:11:42
【问题描述】:

我尝试在我的 Angular 10 应用程序中从 ngx-bootstrap 实现分页。目前记录数为 93 条,每页显示 93 条记录,共 10 页,一次显示 5 页。一次显示 5 页是正确的,因为 maxsize 设置为 5。不知道出了什么问题 itemsperpage 因为我已将其设置为 5,但它显示所有 93

 <div *ngIf="customerDetails">
        <table id="customerdetails-table" class="table table-bordered table-striped">
          <thead>
            <th>Customer</th>
            <th>Company Name</th>
            <th>Contact Name</th>
            <th>Address</th>
            <th>City</th>
            <th>Postal Code</th>
            <th>Country</th>
            <th>Phone</th>
            <th>View Order</th>
          </thead>
          <tbody>
            <tr *ngFor="let custDetails of customerDetails">
              <td>{{custDetails.customerId}}</td>
              <td>{{custDetails.companyName}}</td>
              <td>{{custDetails.contactName}}</td>
              <td>{{custDetails.address}}</td>
              <td>{{custDetails.city}}</td>
              <td>{{custDetails.postalCode}}</td>
              <td>{{custDetails.country}}</td>
              <td>{{custDetails.phone}}</td>
              <td>{{custDetails.customerId}}</td>
            </tr>
          </tbody>
        </table>
        <pagination [totalItems]="totalItems" [itemsPerPage] = "5"  [maxSize]="maxSize" ></pagination>

      </div>

组件

 totalItems: number;
  maxSize = 5;

  ngOnInit(): void {
    this.getCustomerDetails();
  }

  getCustomerDetails(): void {
    this.customerDetailsService.getCustomerDetails()
      .subscribe(  data => { this.customerDetails =  data;
                             this.totalItems = data.length;
      }
    );
  }

【问题讨论】:

    标签: angular ngx-bootstrap


    【解决方案1】:

    你可以像下面这样使用管道

    PaginationPipePipe

    import { Pipe, PipeTransform } from '@angular/core';
    
    @Pipe({
      name: 'paginationPipe'
    })
    export class PaginationPipePipe implements PipeTransform {
      transform(value: any[], currentPage:number,perpage:number): any {
        let result =  value.filter((curr,index)=>{
          return index >= (currentPage-1)*perpage && index < currentPage *perpage
        }); 
        return result;
      }
    }
    

    在 html 中使用这样的管道,

    <tr *ngFor="let custDetails of (customerDetails | paginationPipe:currentPage:itemsPerPage)">
    

    然后给分页一个事件如下

    <pagination [totalItems]="customerDetails.length" (pageChanged)="pageChanged($event)" [itemsPerPage] = "5"  [maxSize]="5" ></pagination>
    

    在组件中更改您发送到管道的当前索引

     pageChanged(event:any) {
       this.currentPage = event.page;
    }
    

    Demo 在这里

    【讨论】:

      【解决方案2】:

      看起来你需要另一个属性来存储页面的当前索引。 像这样的。

      并且每次从(currentPage - 1) * itemsPerPage 循环到currentPage * itemsPerPage

      模板

       <div *ngIf="customerDetails">
              <table id="customerdetails-table" class="table table-bordered table-striped">
                <thead>
                  <th>Customer</th>
                  <th>Company Name</th>
                  <th>Contact Name</th>
                  <th>Address</th>
                  <th>City</th>
                  <th>Postal Code</th>
                  <th>Country</th>
                  <th>Phone</th>
                  <th>View Order</th>
                </thead>
                <tbody>
                  <tr *ngFor="let custDetails of currentPageCustomers">
                    <td>{{custDetails.customerId}}</td>
                    <td>{{custDetails.companyName}}</td>
                    <td>{{custDetails.contactName}}</td>
                    <td>{{custDetails.address}}</td>
                    <td>{{custDetails.city}}</td>
                    <td>{{custDetails.postalCode}}</td>
                    <td>{{custDetails.country}}</td>
                    <td>{{custDetails.phone}}</td>
                    <td>{{custDetails.customerId}}</td>
                  </tr>
                </tbody>
              </table>
              <pagination 
                 [currentPage]="currentPage"
                 (pageChange)="pageChanged($event)"
                 [totalItems]="totalItems" 
                 [itemsPerPage] = "itemsPerPage"  
                 [maxSize]="maxSize">
              </pagination>
      
      </div>
      

      组件

       currentPage = 1;
       itemsPerPage = 5;
       totalItems: number;
       maxSize = 5;
       currentPageCustomers = [];
      
        ngOnInit(): void {
          this.getCustomerDetails();
        }
      
        getCustomerDetails(): void {
          this.customerDetailsService
            .getCustomerDetails()
            .subscribe(data => { 
               this.customerDetails = data;
               this.totalItems = data.length;
               if(this.totalItems) {
                  // call pageChanged function for define currentPageCustomers property 
                  pageChanged(1);
               }
            }
          );
        }
      
      pageChanged(currentPageIndex: number) {
         // currentPageIndex starts at 1
         this.currentPage = currentPageIndex;
         // for example in case of currentPage = 1;
         // the code will be slice from (1 - 1) * 5 to 1 * 5
         // from 0 to 5
         this.currentPageCustomers = this.customerDetails
            .slice(
               (this.currentPage - 1) * this.itemsPerPage,
               this.currentPage * this.itemsPerPage
            );
      
      }
      
      

      【讨论】:

      • 您在模板中放入的任何表达式,将在每个 changeDetection 周期重新计算,例如。 *ngFor="let custDetails of customerDetails.slice((currentPage - 1) * itemsPerPage,currentPage * itemsPerPage)"。出于这个原因,我会在pageChanged 方法中计算当前项目的数组,并在ngFor 中使用它。组件类中的东西也更容易测试。此外,我会将trackBy 添加到ngFor
      • @AlexBiro 是的,我会在有机会时更新我的​​答案。你可以有一个额外的阵列。并在pageChanged 函数中定义它。
      【解决方案3】:

      您拥有页面上的所有元素,因为该组件不会划分您的元素列表,这是您或服务器的工作。 该组件只管理显示和分页状态。

      【讨论】:

        猜你喜欢
        • 2012-12-26
        • 2017-09-02
        • 2018-10-27
        • 2013-04-21
        • 1970-01-01
        • 2020-05-04
        • 1970-01-01
        • 1970-01-01
        • 2023-02-03
        相关资源
        最近更新 更多