Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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":"[email protected]"},
{"name":"sumit","email":"[email protected]"},
{"name":"mohit","email":"[email protected]"},
{"name":"rajesh","email":"[email protected]"},
{"name":"ramesh","email":"[email protected]"},
{"name":"vikas","email":"[email protected]"},
{"name":"suresh","email":"[email protected]"},
{"name":"ramesh","email":"[email protected]"},
];

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?

like image 966
Akash Avatar asked May 22 '20 09:05

Akash


1 Answers

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

like image 76
IAfanasov Avatar answered Sep 30 '22 19:09

IAfanasov