How to use Ngx-pagination on table in angular 7

眉间皱痕 提交于 2020-01-04 07:28:10

问题


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.


回答1:


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>


来源:https://stackoverflow.com/questions/59316363/how-to-use-ngx-pagination-on-table-in-angular-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!