I am new to Angular 4 and I would appreciate if somebody could tell me how I can add pagination in a table. Below is my code:
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>
And my JSON is :
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" }
]
}
]
I want to add 5 items per page in a table. Please help me in this regard.
Pagination. To paginate the table's data, add a <mat-paginator> after the table. If you are using the MatTableDataSource for your table's data source, simply provide the MatPaginator to your data source.
A simple solution with ngx-pagination installed for Angular 4 can be found here. All you need is to import it to your ngModule and declare the page index in your component.ts as p: number = 1;
.
Locate your pagination element under your table as shown below:
<pagination-controls (pageChange)="p = $event"></pagination-controls>
Declare the number of row to be shown in your *ngFor //:
<tr *ngFor="let game of games | paginate: { itemsPerPage: 5, currentPage: p }; let i = index">
Hope it helps you!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With