为表angular5添加分页

时间:2018-04-27 10:10:38

标签: angular5 ngx-pagination

我想为表格添加分页我尝试过ngx-pagination但似乎无法使用angular5: 只是一个例子

<table>
   <tr *ngFor="let x in names">
    <td>{{ x.Name }}</td>
    <td>{{ x.Country }}</td>
   </tr>
 </table>

已经搜索了很多,但发现了这个:

ngx-pagination

这个包在导入说:

时给出了错误

无法解析符号NgxPaginationModule

我正在使用 angular5 的package.json

    {
  "name": "eci",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "node app.js",
    "build": "ng build --prod",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^5.2.0",
    "@angular/common": "^5.2.0",
    "@angular/compiler": "^5.2.0",
    "@angular/core": "^5.2.0",
    "@angular/forms": "^5.2.0",
    "@angular/http": "^5.2.0",
    "@angular/platform-browser": "^5.2.0",
    "@angular/platform-browser-dynamic": "^5.2.0",
    "@angular/router": "^5.2.0",
    "angular-font-awesome": "^3.1.2",
    "angular2-json2csv": "^1.1.2",
    "angular5-csv": "^0.2.8",
    "bootstrap": "^4.1.0",
    "cfenv": "^1.0.4",
    "core-js": "^2.4.1",
    "express": "^4.15.0",
    "font-awesome": "^4.7.0",
    "ngx-pagination": "^3.1.1",
    "rxjs": "^5.5.6",
    "zone.js": "^0.8.19"
  },
  "devDependencies": {
    "@angular/cli": "~1.7.2",
    "@angular/compiler-cli": "^5.2.0",
    "@angular/language-service": "^5.2.0",
    "@types/jasmine": "~2.8.3",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "^4.0.1",
    "jasmine-core": "~2.8.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~2.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~4.1.0",
    "tslint": "~5.9.1",
    "typescript": "~2.5.3"
  }
}

3 个答案:

答案 0 :(得分:1)

尝试在app.module.ts中导入 NgxPaginationModule 。试试这个

// 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 {}

答案 1 :(得分:1)

({at first sorry for my bad english) 为此,您可以执行以下操作: (1)使用以下命令添加库:npm install ngx-pagination@3.1.1 --save(@ 3.1.1->以防万一,您必须使用此版本) 接着 (2)将此库导入到您的app.module.ts中(执行此操作后,您的app.module如下所示):

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { CommonModule } from '@angular/common';
import { NgxPaginationModule } from 'ngx-pagination';  /* import NgxPaginationModule here */

@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule,
        AppRoutingModule,
        CommonModule,     /* always check has this line this */
        NgxPaginationModule    /* add NgxPaginationModule to your imports */
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule { }

并将其添加到要使用此模块的组件中:(我将其添加到我的app.component.ts中):     页= 1; 更改后我的app.component.ts是:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  // Your Data
  names: any = [
    {
      Name: 'frank',
      Country: 'US'
    },
    {
      Name: 'joe',
      Country: 'fanland'
    },
    {
      Name: 'mary',
      Country: 'nourthAmerica'
    },
    {
      Name: 'elina',
      Country: 'US'
    },
    {
      Name: 'emmi',
      Country: 'Argentina'
    },
    {
      Name: 'jully',
      Country: 'Belize'
    },
    {
      Name: 'mariana',
      Country: 'Cameroun'
    },
    {
      Name: 'shivana',
      Country: 'Costa Rica'
    },
    {
      Name: 'jullietta',
      Country: 'Danemark'
    }];
  // number
  page = 1;    /* this show page in pagination controller after page load */
  constructor() {}
}

现在如何使用它? 像管道一样使用此模块,并在模板中给它一个分页控件标签(我在app.component.html中使用它)

<table>
  <tr *ngFor="let x of names | paginate: { itemsPerPage: 3, currentPage: page }">
    <td>{{ x.Name }}</td>
    <td>{{ x.Country }}</td>
  </tr>
</table>
<pagination-controls (pageChange)="page = $event"></pagination-controls>

我希望这会有所帮助。

答案 2 :(得分:0)

我按照以下步骤解决了这个问题(Windows 操作系统用户):

  1. 使用 VS Code 作为管理员模式打开您的 Angular 项目。
  2. 在终端中运行 npm install 并等待它完成。
  3. 关闭VS代码。
  4. 重新打开您的 Angular 项目,使用 VS Code 作为管理员模式

问题应该得到解决。