'Angular Client Side Pagination
I am pulling an array of data from database and displaying it with a simple table.What i am trying to do is adding a pagination feature to this table.For example in each page there will be 5 rows.In this case courseData is the array.
<table style="border-spacing: 5rem">
<tr >
<td style="font-size: 20px;">CRN</td>
<pre> </pre>
<td style = "font-size: 20px;" >Name</td>
<pre> </pre>
<td style ="font-size: 20px;" >Lecturer</td>
<pre> </pre>
<td style = "font-size: 20px;" >Level</td>
<pre> </pre>
<td style = "font-size: 20px;" >Days</td>
<pre> </pre>
<td style = "font-size: 20px;" >Time</td>
</tr>
<ng-container *ngFor="let data of courseData">
<tr>
<td style="text-decoration: underline;" (click)="crnClicked(data.crn)">{{data.crn}}</td>
<pre> </pre>
<td *ngIf="data.specialapp !=1" >{{data.name}}</td>
<td style="font-weight: bold;"*ngIf="data.specialapp==1" >{{data.name}}</td>
<pre> </pre>
<td>{{data.lecturer}}</td>
<pre> </pre>
<td>{{data.level}}</td>
<pre> </pre>
<td>{{data.days}}</td>
<pre> </pre>
<td>{{data.hours}}</td>
<button *ngIf="data.specialapp!=1" ion-button small round (click)="addCrn(data.crn)" color="primary" block>+</button>
</tr>
</ng-container>
</table>
Solution 1:[1]
You can use ngx-pagination module. Please try
<ng-container *ngFor="let data of courseData | paginate: { id: 'foo',
itemsPerPage: pageSize,
currentPage: p,
totalItems: total>
</ng-container>
app.module.ts:
import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {NgxPaginationModule} from 'ngx-pagination'; // <-- import the module
import {MyComponent} from './my.component';
@NgModule({
imports: [BrowserModule, NgxPaginationModule], // <-- include it in your app module
declarations: [MyComponent],
bootstrap: [MyComponent]
})
export class MyAppModule {}
my.component.ts:
import {Component} from '@angular/core';
@Component({
selector: 'my-component',
template: `
<ul>
<li *ngFor="let item of collection | paginate: { itemsPerPage: 10, currentPage: p }"> ... </li>
</ul>
<pagination-controls (pageChange)="p = $event"></pagination-controls>
`
})
export class MyComponent {
p: number = 1;
collection: any[] = someArrayOfThings;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Arielle Nguyen |