【发布时间】:2018-04-28 08:25:33
【问题描述】:
我在 https://www.npmjs.com/package/ngx-pagination 的文档中一步一步地遵循了 Pagination for Angular 的说明。
我的分页工作完美,我对此没有任何问题。但是,由于我正在使用大型数据集 - 我不想使用内存中的完整集合,并且需要某种服务器端分页,服务器一次只发送一个页面。如那篇文章所述,我应该使用totalItems 参数并使用计数,但我不知道如何?我应该如何设置total?
<table class='table' *ngIf="collection">
<tbody>
<tr *ngFor="let item of collection |
paginate: { itemsPerPage: 10, currentPage: p, totalItems: total }">
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
</tr>
</tbody>
而我的WEB API是这样的:
[HttpGet("[action]")]
public async Task<IEnumerable<MyClass>> MyMethod()
{
int perPage = 10;
int start = (page - 1) * perPage;
int end = start + perPage;
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("externalAPI");
MediaTypeWithQualityHeaderValue contentType =
new MediaTypeWithQualityHeaderValue("application/json");
client.DefaultRequestHeaders.Accept.Add(contentType);
HttpResponseMessage response = await client.GetAsync(client.BaseAddress);
string content = await response.Content.ReadAsStringAsync();
List<MyClass> data = JsonConvert.DeserializeObject<List<MyClass>>(content);
return data.Skip(start).Take(perPage).ToList();
}
}
还有:
p: number = 1;
total: number;
http.get('url', {
params: {
page : this.p
}
}).subscribe(result => {
this.collections = result.json() as Collection[];
}, error => console.error(error));
【问题讨论】:
标签: c# angular asp.net-web-api asp.net-core-mvc