Kumail Hussain
Kumail Hussain

Reputation: 879

add pagination for table angular5

I want to add pagination for table i have tried ngx-pagination but it seems to be not working with angular5: just an example

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

have searched a lot but found this:


ngx-pagination

this package gives me the error while importing saying :

cannot resolve symbol NgxPaginationModule

I am using 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"
  }
}

Upvotes: 1

Views: 2818

Answers (3)

Kevy Granero
Kevy Granero

Reputation: 783

I solve this issue by following the steps below (Windows OS user):

  1. Open your Angular project using VS Code as Administrator mode.
  2. Run npm install in the Terminal and wait it to complete.
  3. Close VS Code.
  4. Re-open your Angular project using VS Code as Administrator mode.

The issue should be fixed.

Upvotes: 0

Ali.ro
Ali.ro

Reputation: 54

(at first sorry for my bad english) For this to work you can do this: ( 1 ) add library with this command: npm install [email protected] --save ( @3.1.1 -> just in case you must use this version) and then ( 2 ) import this library to your app.module.ts (after doing this, your app.module looks like this):

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

and add this to your component in which you want to use this module:(i add it to my app.component.ts): page = 1; and my app.component.ts after this change is:

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() {}
}

Now how to use it? Use this module like pipes and give it a pagination-controls tag in your template (I use it in 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>

I hope this was helpful.

Upvotes: 1

Vignesh
Vignesh

Reputation: 2386

Try to import NgxPaginationModule in app.module.ts. Try this

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

Upvotes: 1

Related Questions