Search code examples
angularbootstrap-4bootstrap-modal

I am using ngb Pagination in my angular app but it is not working properly


I have applied NGB Pagination on my angular app in a table, but it is not working. It is just showing 2 pages and not dividing the data between pages of table. My code is below:

import { UserService } from './../services/user.service';
import { Component, OnInit } from '@angular/core';
import { UserInfo } from 'src/app/models/user-info';

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.scss']
})
export class UserComponent implements OnInit {

  constructor(private _userService:UserService) { }

  ngOnInit() {
    this.getUserData();

  }


userData=[
{"name":"Akash","email":"akash@gmmail.com"},
{"name":"sumit","email":"sumit@gmmail.com"},
{"name":"mohit","email":"mohit@gmmail.com"},
{"name":"rajesh","email":"rajesh@gmmail.com"},
{"name":"ramesh","email":"ramesh@gmmail.com"},
{"name":"vikas","email":"vikas@gmmail.com"},
{"name":"suresh","email":"suresh@gmmail.com"},
{"name":"ramesh","email":"ramesh@gmmail.com"},
];

elementPerpage:number=5;
currentPage:number=1;
arrayLength:number=this.userData.length;
  userInfo:UserInfo[];

this my Html File

<h3>User Details</h3>
<div class="table-responsive">
    <table class="table table-striped ">
        <thead>
            <tr>
                <th scope="col">User Name</th>
                <th scope="col">User Email</th>
            </tr>
        </thead>
        <tbody>
            <tr *ngIf="!userData || userData.length === 0">
                <td colspan="2" class="text-center">No records</td>
            </tr>
            <tr *ngFor="let info of userData">
                <td>
                    {{info.name}}
                </td>
                <td>
                   {{info.email}}
                </td>
            </tr>
        </tbody>
    </table>
</div>
<div >
    <ngb-pagination [collectionSize]="arrayLength" [(page)]="currentPage" [pageSize]="elementPerpage">
    </ngb-pagination>
</div>

I have added Bootstrap to the project there no errors. But pagination is not working. How can I make ngb pagination work?


Solution

  • Please take a look at the pagination docs. The basic usage example:

    <table>
      <tr *ngFor="let item of items | slice: (page-1) * pageSize : (page-1) * pageSize + pageSize">
        <!-- content here -->
      </tr>
    </table>
    
    <ngb-pagination
      [(page)]="page"
      [pageSize]="pageSize"
      [collectionSize]="items.length"></ngb-pagination>
    

    *ngFor contains an answer (: