如何在Angular 4中为表添加分页?

Neh*_*yal 9 html pagination html-table angular

我是Angular 4的新手,如果有人能告诉我如何在表格中添加分页,我将不胜感激.以下是我的代码:

HTML:

<div *ngIf="tableDiv && adv1" id="adv1Div">
            <table class="table table-hover" id="adv1Table">
                <thead>
                    <tr class="black-muted-bg" >
                        <th class="align-right" *ngFor="let data of calendarTable[0].weeks">{{data.period}}</th>

                    </tr>
                </thead>
                <tbody>
                    <tr class="no-top-border" *ngFor="let item of calendarTable| paginate: { itemsPerPage: 9, currentPage: p };">
                        <td contenteditable='true' *ngFor="let data of item.weeks| paginate: { itemsPerPage: 9, currentPage: p };" class="align-right">{{data.price}}</td>   
                    </tr>
                </tbody>
                <a href="javascript:void(0)">
                    {{p}}
                  </a>

            </table>

        </div>         
Run Code Online (Sandbox Code Playgroud)

我的JSON是:

 public calendarTable = [
      { name: "TV AD1", 
          weeks: [
          { period: "2/10/2017", price: "400" },
          { period: "9/10/2017", price: "800" },
          { period: "16/10/2017", price: "200" },
          { period: "23/10/2017", price: "500" },
          { period: "30/10/2017", price: "600" },
          { period: "6/11/2017", price: "800" },
          { period: "13/11/2017", price: "700" },
          { period: "20/11/2017", price: "800" },
          { period: "27/11/2017", price: "900" },
          { period: "4/12/2017", price: "400" },
          { period: "11/12/2017", price: "800" },
          { period: "18/12/2017", price: "200" },
          { period: "25/12/2017", price: "500" },
          { period: "1/1/2018", price: "600" },
          { period: "8/1/2018", price: "800" },
          { period: "15/1/2018", price: "700" },
          { period: "22/1/2018", price: "800" },
          { period: "29/1/2018", price: "900" }
          ]

          }
   ]
Run Code Online (Sandbox Code Playgroud)

我想在表格中每页添加5个项目.请帮助我这方面.

Mne*_*emo 17

可以在此处找到为Angular 4安装ngx-pagination的简单解决方案.您只需将其导入ngModule并在component.ts中声明页面索引即可p: number = 1;.

在表格下找到您的分页元素,如下所示:

<pagination-controls (pageChange)="p = $event"></pagination-controls>
Run Code Online (Sandbox Code Playgroud)

声明要在*ngFor //中显示的行数:

<tr *ngFor="let game of games | paginate: { itemsPerPage: 5, currentPage: p }; let i = index">
Run Code Online (Sandbox Code Playgroud)

希望它能帮到你!

  • 这对我来说很好。谢谢 (2认同)
  • 这是一个非常简单的解决方案,非常感谢! (2认同)

Ami*_*mit 1

如果需要,您可以使用 Angular DataTable。

对于文档:

https://www.npmjs.com/package/angular2-datatable