angularnpmpaginationangular7ngx-pagination

How to use Ngx-pagination on table in angular 7


Hi I have a list like,

[{…}, {…}, {…}]

0: {name: "Manu", age: "21", hobbies: Array(4)}

1: {name: "Anu", age: "20", hobbies: Array(3)}

2: {name: "nandu", age: "22", hobbies: Array(5)}

I need to show this on a table.So i am doing the code below

<table id='studTable'>
<thead>
    <tr>
        <th style="text-align: center">Student Name </th>
        <th style="text-align: center">Age</th>
        <th style="text-align: center">Hobbies</th>
    </tr>
</thead>
<tbody>
    <tr *ngFor="let student of students | paginate: { itemsPerPage: 10, currentPage: p } ; let i = index">
        <td>
            <input matInput [(ngModel)]="students[i].name" name="name{{i}}">
        </td>
        <td><input matInput type="text" [(ngModel)]="students[i].age" name="age{{i}}"></td> 
        <td>
            <mat-select [(ngModel)]="students[i].hobbies" name="hobbies{{i}}" multiple>
                <mat-option *ngFor="let hobbie of studHobbies" [value]="hobbie.studHobbie">
                    {{hobbie.studHobbie}}
                </mat-option>
            </mat-select>
        </td>
    </tr></tbody></table><pagination-controls (pageChange)="p = $event"></pagination-controls>

But When i doing this,I am getting a bug like ,

The first row of table is showing when i am pressing next page on pagination.But the Number of Items to be shown is correct,ie if I have 5 Items to show ,then pagination control div will show 5 pages ,But first record is repeating in each pages.

I took https://www.freakyjolly.com/angular-7-6-pagination-implement-local-or-server-pagination-in-3-steps/ for a reference of pagination.

And https://stackoverflow.com/a/48293486/9493078 Also.


Solution

  • Replace students[i] as student

    <tr *ngFor="let student of students | paginate: { itemsPerPage: 10, currentPage: p } ; let i = index">
        <td>
            <input matInput [(ngModel)]="student.name" name="name{{i}}">
                </td>
        <td><input matInput type="text" [(ngModel)]="student.age" name="age{{i}}"></td>
        <td>
            <mat-select [(ngModel)]="student.hobbies" name="hobbies{{i}}" multiple>
                <mat-option *ngFor="let hobbie of studHobbies" [value]="hobbie.studHobbie">
                    {{hobbie.studHobbie}}
                </mat-option>
            </mat-select>
        </td>
    </tr>