Joz*_*chy 9 ng-bootstrap angular
我有一个Angular2应用程序component,我有一张桌子.表是通过*ngFor指令生成的.该行的每一行table都是一个对象,当初始化组件时,该对象将从后端加载9个字段.在应用程序中,我正在尝试使用ng-bootstrap进行角度模块.
ng-boorstrap
特别是我正在尝试实现该pagination组件.
有人可以解释如何放置代码,以便它可以渲染,例如每页只有10行吗?或者给我一个实现完成的参考.
我所做的是:
NgbModule引用放在我的模块中,我将声明我的组件以及NgbPaginationConfig模块(使用自定义分页所必需的)把ngb-pagination代码放在我component这样的视图中
<table class="table table-striped">
<thead>
<tr>
<th>Tracking #</th>
<th>Brand</th>
<th>Geography</th>
<th>Country</th>
<th>Contract Name</th>
<th>Project Name</th>
<th>Status</th>
<th>$US BMC</th>
<th>Release #</th>
<th id="column-last"></th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of viewRows ">
<td>{{item.trackingNr}}</td>
<td>{{item.brand}}</td>
<td>{{item.geo}}</td>
<td>{{item.country}}</td>
<td>{{item.contractName}}</td>
<td>{{item.projectName}}</td>
<td>{{item.status}}</td>
<td>{{item.usBmc}}</td>
<td>{{item.releaseNr}}</td>
<td id="column-last">
<span class="awficon-edit" id="row-icons"></span>
<span class="awficon-close-2" id="row-icons"></span>
</td>
</tr>
</tbody>
Run Code Online (Sandbox Code Playgroud)
Иго*_*нюк 29
这是我的工作解决方案.ngb-pagination的API:https://ng-bootstrap.github.io/#/components/pagination
...
</table>
<ngb-pagination [collectionSize]="totalItems" [pageSize]="itemsPerPage" [(page)]="page" [maxSize]="7" [rotate]="true" (pageChange)="loadPage($event)"></ngb-pagination>
Run Code Online (Sandbox Code Playgroud)
在你的组件中你需要一些这样的东西.不要忘记在构造函数中设置变量:
itemsPerPage: number;
totalItems: any;
page: any;
previousPage: any;
...
loadPage(page: number) {
if (page !== this.previousPage) {
this.previousPage = page;
this.loadData();
}
}
...
loadData() {
this.dataService.query({
page: this.page - 1,
size: this.itemsPerPage,
}).subscribe(
(res: Response) => this.onSuccess(res.json(), res.headers),
(res: Response) => this.onError(res.json())
)
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找这个问题的答案,似乎没有人发布明确的答案。
这是我的解决方案:
在Angular 7中使用ngFor进行ngb分页
export class HomeComponent implements OnInit {
currentPage = 1;
itemsPerPage = 5;
pageSize: number;
constructor() { }
public onPageChange(pageNum: number): void {
this.pageSize = this.itemsPerPage*(pageNum - 1);
}
public changePagesize(num: number): void {
this.itemsPerPage = this.pageSize + num;
}
}Run Code Online (Sandbox Code Playgroud)
<div class="container-fluid">
<div class="col-6 input-group">
<div class="col-5 input-group-addon">
<ngb-pagination [collectionSize]="users.length" #numPages [pageSize]="itemsPerPage" [(page)]="currentPage" (pageChange)="onPageChange(currentPage)"></ngb-pagination>
</div>
<div class="col-1 input-group-addon">
<input class="input-sm text-center" type="number" [min]="10" [max]="users.length" step="1" [(ngModel)]="itemsPerPage" (onClick)="changePagesize(pageSize)">
</div>
</div>
<ul *ngIf="users">
<li *ngFor="let user of users | slice: pageSize | slice: 0:itemsPerPage">
<img [src]="user.avatar" alt="{{ user.avatar }}">
<p>{{ user.id }}. {{ user.first_name }} {{ user.last_name }}</p>
</li>
</ul>
<pre><span class="float-md-left">Page: {{ currentPage }} / {{numPages.pageCount}}</span><span class="float-md-right">Found items: {{ itemsPerPage }} / {{ users.length }}</span></pre>
</div>Run Code Online (Sandbox Code Playgroud)
此解决方案也适用于Angular6。我尚未在其他版本上对其进行测试。
有关更多信息,请参阅文档。Unfortunatley ...它缺少有关ngFor迭代的重要信息。
我遇到的另一个问题是如何设置页数。我决定为有相同问题的人添加完整的解决方案。我在这里找到了这个答案。注意上面的标识符#numPages。这是关于Stackblitz的现场演示
| 归档时间: |
|
| 查看次数: |
23974 次 |
| 最近记录: |