角度客户端分页

时间:2018-05-07 14:43:10

标签: angular pagination

我从数据库中提取数据并使用简单的表格显示它。我想要做的是为此表添加分页功能。例如,在每个页面中将有5行。在这种情况下,courseData是数组。

<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>

1 个答案:

答案 0 :(得分:5)

您可以使用ngx-pagination模块。请尝试

<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;  
}