如何在 angular 7 的表格上使用 Ngx 分页 [英] How to use Ngx-pagination on table in angular 7

查看:16
本文介绍了如何在 angular 7 的表格上使用 Ngx 分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,例如

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

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

0:{name:Manu",年龄:21",爱好:Array(4)}

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

1:{姓名:Anu",年龄:20",爱好:Array(3)}

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

2:{name:nandu",年龄:22",爱好:Array(5)}

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 ,

当我在分页上按下一页时显示表格的第一行.但是要显示的项目数是正确的,即如果我有 5 个项目要显示,那么分页控制 div 将显示 5 页,但首先记录在每一页中重复.

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.

我采用了 https://www.freakyjolly.com/angular-7-6-pagination-implement-local-or-server-pagination-in-3-steps/ 分页参考.

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

还有 https://stackoverflow.com/a/48293486/9493078 也是.

推荐答案

将学生 [i] 替换为学生

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>

这篇关于如何在 angular 7 的表格上使用 Ngx 分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆