【发布时间】:2019-08-22 21:44:01
【问题描述】:
我想为我的应用使用primeng 分页。我指的是this doc。但它不起作用。我有 10 条记录,我给行的值为 2,但它在一页上显示了所有 10 条记录。
HTML 代码
<div class="p-grid">
<div class="p-col">
<p-table #table [value]="coins" [paginator]="true" [rows]="2" [responsive]="true">
<ng-template pTemplate="header">
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</ng-template>
<ng-template pTemplate="body">
<tr *ngFor="let coin of coins">
<td>{{coin.id}}</td>
<td>{{coin.name}}</td>
</tr>
</ng-template>
</p-table>
TestComponent.ts
import { Component, OnInit } from '@angular/core';
import { CryptoService } from 'src/app/services/crypto.service';
@Component({
selector: 'app-test',
templateUrl: './test.component.html',
styleUrls: ['./test.component.scss']
})
export class TestComponent implements OnInit {
coins = [];
constructor(private cryptoservice: CryptoService)
{
this.coins = cryptoservice.getMyItems();
}
ngOnInit() {
}
}
CrptoService.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class CryptoService {
coins= [
{id: 1, name: 'BTC'},
{id: 2, name: 'XRP'},
{id: 3, name: 'SSS'},
{id: 4, name: 'ERT'},
{id: 5, name: 'KKK'},
{id: 6, name: 'LLL'},
{id: 7, name: 'III'},
{id: 8, name: 'OOO'},
{id: 9, name: 'PPPP'}
];
constructor() { }
getMyItems()
{
return this.coins;
}
}
【问题讨论】:
-
您没有按照您的示例提供的那样执行此操作。请尝试使用 columns 和 rowData
-
你正在使用 [value]="coins" 和 *ngFor="let coin of coin"。因此,您以这种方式提供了 2 次数据。在正文模板中,您应该使用文档中的“let-rowData”。