How to implement Bootstrap 4 for Angular 2 ngb-pagination
I have a Angular2
app with a component
where I have a table.
Table is generated via *ngFor
directive.
Each row of the table
is an object with 9 fields that is being loaded from the backend when the component is initialized.
In the app I'm trying to use ng-bootstrap for angular module.
ng-boorstrap
In particular I'm trying to implement the pagination
component.
Could somebody explain how to put the code so it would render e.g. only 10 rows per page pls? Or give me a reference where the implementation is done.
What I have done is:
- to put the
NgbModule
reference to my module where I'm declaring my component as well as theNgbPaginationConfig
module (necessary for using custom pagination)
put the
ngb-pagination
code in the view of mycomponent
like this
<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>
angular ng-bootstrap
add a comment |
I have a Angular2
app with a component
where I have a table.
Table is generated via *ngFor
directive.
Each row of the table
is an object with 9 fields that is being loaded from the backend when the component is initialized.
In the app I'm trying to use ng-bootstrap for angular module.
ng-boorstrap
In particular I'm trying to implement the pagination
component.
Could somebody explain how to put the code so it would render e.g. only 10 rows per page pls? Or give me a reference where the implementation is done.
What I have done is:
- to put the
NgbModule
reference to my module where I'm declaring my component as well as theNgbPaginationConfig
module (necessary for using custom pagination)
put the
ngb-pagination
code in the view of mycomponent
like this
<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>
angular ng-bootstrap
add a comment |
I have a Angular2
app with a component
where I have a table.
Table is generated via *ngFor
directive.
Each row of the table
is an object with 9 fields that is being loaded from the backend when the component is initialized.
In the app I'm trying to use ng-bootstrap for angular module.
ng-boorstrap
In particular I'm trying to implement the pagination
component.
Could somebody explain how to put the code so it would render e.g. only 10 rows per page pls? Or give me a reference where the implementation is done.
What I have done is:
- to put the
NgbModule
reference to my module where I'm declaring my component as well as theNgbPaginationConfig
module (necessary for using custom pagination)
put the
ngb-pagination
code in the view of mycomponent
like this
<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>
angular ng-bootstrap
I have a Angular2
app with a component
where I have a table.
Table is generated via *ngFor
directive.
Each row of the table
is an object with 9 fields that is being loaded from the backend when the component is initialized.
In the app I'm trying to use ng-bootstrap for angular module.
ng-boorstrap
In particular I'm trying to implement the pagination
component.
Could somebody explain how to put the code so it would render e.g. only 10 rows per page pls? Or give me a reference where the implementation is done.
What I have done is:
- to put the
NgbModule
reference to my module where I'm declaring my component as well as theNgbPaginationConfig
module (necessary for using custom pagination)
put the
ngb-pagination
code in the view of mycomponent
like this
<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>
angular ng-bootstrap
angular ng-bootstrap
edited Dec 19 '16 at 16:08
4444
3,33682443
3,33682443
asked Dec 19 '16 at 13:13
Jozef PlachyJozef Plachy
35116
35116
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
it's my working solution.
API for ngb-pagination: 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>
In your component you need some like that. Don't forget set your variable in constructor:
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())
)
}
1
The component documentation lacks important details that you have covered. Thank you.
– Paulo Pedroso
Jul 30 '17 at 16:36
how are you getting the previous page? I see the type any. but am missing where you are assigning a value.
– Winnemucca
Oct 1 '17 at 8:02
I get params in routerResolver, for example:resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { const page = route.queryParams['page'] ? route.queryParams['page'] : '1'; const sort = route.queryParams['sort'] ? route.queryParams['sort'] : 'id,asc'; return { page: page }; }
– Игорь Демянюк
Oct 2 '17 at 7:34
I use it for prevent load current page (if user load 6 page and click on 6, data will reload.So i subscribe to params in constuctor. ``` this.routeData = this.activatedRoute.data.subscribe((data) => { this.page = data['pagingParams'].page; this.previousPage = data['pagingParams'].page; }); ```
– Игорь Демянюк
Oct 2 '17 at 9:45
add a comment |
I was searching for answers to this same question and nobody seems to post a clear answer.
Here's my solution:
ngbPagination with ngFor in Angular 7
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;
}
}
<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>
This solution also applies to Angular 6. I have not tested it on other versions.
For more information, check the documentation. Unfortunatley... it lacks vital information regarding ngFor iteration.
Another problem I had was how to set the number of pages. I decided to add my complete solution for those having the same problem. I found this answer here. Take note of the identifier #numPages above.
add a comment |
Ok. I found an awesome solution in github.
Check it out.
https://github.com/michaelbromley/ng2-pagination
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41223400%2fhow-to-implement-bootstrap-4-for-angular-2-ngb-pagination%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
it's my working solution.
API for ngb-pagination: 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>
In your component you need some like that. Don't forget set your variable in constructor:
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())
)
}
1
The component documentation lacks important details that you have covered. Thank you.
– Paulo Pedroso
Jul 30 '17 at 16:36
how are you getting the previous page? I see the type any. but am missing where you are assigning a value.
– Winnemucca
Oct 1 '17 at 8:02
I get params in routerResolver, for example:resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { const page = route.queryParams['page'] ? route.queryParams['page'] : '1'; const sort = route.queryParams['sort'] ? route.queryParams['sort'] : 'id,asc'; return { page: page }; }
– Игорь Демянюк
Oct 2 '17 at 7:34
I use it for prevent load current page (if user load 6 page and click on 6, data will reload.So i subscribe to params in constuctor. ``` this.routeData = this.activatedRoute.data.subscribe((data) => { this.page = data['pagingParams'].page; this.previousPage = data['pagingParams'].page; }); ```
– Игорь Демянюк
Oct 2 '17 at 9:45
add a comment |
it's my working solution.
API for ngb-pagination: 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>
In your component you need some like that. Don't forget set your variable in constructor:
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())
)
}
1
The component documentation lacks important details that you have covered. Thank you.
– Paulo Pedroso
Jul 30 '17 at 16:36
how are you getting the previous page? I see the type any. but am missing where you are assigning a value.
– Winnemucca
Oct 1 '17 at 8:02
I get params in routerResolver, for example:resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { const page = route.queryParams['page'] ? route.queryParams['page'] : '1'; const sort = route.queryParams['sort'] ? route.queryParams['sort'] : 'id,asc'; return { page: page }; }
– Игорь Демянюк
Oct 2 '17 at 7:34
I use it for prevent load current page (if user load 6 page and click on 6, data will reload.So i subscribe to params in constuctor. ``` this.routeData = this.activatedRoute.data.subscribe((data) => { this.page = data['pagingParams'].page; this.previousPage = data['pagingParams'].page; }); ```
– Игорь Демянюк
Oct 2 '17 at 9:45
add a comment |
it's my working solution.
API for ngb-pagination: 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>
In your component you need some like that. Don't forget set your variable in constructor:
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())
)
}
it's my working solution.
API for ngb-pagination: 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>
In your component you need some like that. Don't forget set your variable in constructor:
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())
)
}
answered May 18 '17 at 8:31
Игорь ДемянюкИгорь Демянюк
711612
711612
1
The component documentation lacks important details that you have covered. Thank you.
– Paulo Pedroso
Jul 30 '17 at 16:36
how are you getting the previous page? I see the type any. but am missing where you are assigning a value.
– Winnemucca
Oct 1 '17 at 8:02
I get params in routerResolver, for example:resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { const page = route.queryParams['page'] ? route.queryParams['page'] : '1'; const sort = route.queryParams['sort'] ? route.queryParams['sort'] : 'id,asc'; return { page: page }; }
– Игорь Демянюк
Oct 2 '17 at 7:34
I use it for prevent load current page (if user load 6 page and click on 6, data will reload.So i subscribe to params in constuctor. ``` this.routeData = this.activatedRoute.data.subscribe((data) => { this.page = data['pagingParams'].page; this.previousPage = data['pagingParams'].page; }); ```
– Игорь Демянюк
Oct 2 '17 at 9:45
add a comment |
1
The component documentation lacks important details that you have covered. Thank you.
– Paulo Pedroso
Jul 30 '17 at 16:36
how are you getting the previous page? I see the type any. but am missing where you are assigning a value.
– Winnemucca
Oct 1 '17 at 8:02
I get params in routerResolver, for example:resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { const page = route.queryParams['page'] ? route.queryParams['page'] : '1'; const sort = route.queryParams['sort'] ? route.queryParams['sort'] : 'id,asc'; return { page: page }; }
– Игорь Демянюк
Oct 2 '17 at 7:34
I use it for prevent load current page (if user load 6 page and click on 6, data will reload.So i subscribe to params in constuctor. ``` this.routeData = this.activatedRoute.data.subscribe((data) => { this.page = data['pagingParams'].page; this.previousPage = data['pagingParams'].page; }); ```
– Игорь Демянюк
Oct 2 '17 at 9:45
1
1
The component documentation lacks important details that you have covered. Thank you.
– Paulo Pedroso
Jul 30 '17 at 16:36
The component documentation lacks important details that you have covered. Thank you.
– Paulo Pedroso
Jul 30 '17 at 16:36
how are you getting the previous page? I see the type any. but am missing where you are assigning a value.
– Winnemucca
Oct 1 '17 at 8:02
how are you getting the previous page? I see the type any. but am missing where you are assigning a value.
– Winnemucca
Oct 1 '17 at 8:02
I get params in routerResolver, for example:
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { const page = route.queryParams['page'] ? route.queryParams['page'] : '1'; const sort = route.queryParams['sort'] ? route.queryParams['sort'] : 'id,asc'; return { page: page }; }
– Игорь Демянюк
Oct 2 '17 at 7:34
I get params in routerResolver, for example:
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { const page = route.queryParams['page'] ? route.queryParams['page'] : '1'; const sort = route.queryParams['sort'] ? route.queryParams['sort'] : 'id,asc'; return { page: page }; }
– Игорь Демянюк
Oct 2 '17 at 7:34
I use it for prevent load current page (if user load 6 page and click on 6, data will reload.So i subscribe to params in constuctor. ``` this.routeData = this.activatedRoute.data.subscribe((data) => { this.page = data['pagingParams'].page; this.previousPage = data['pagingParams'].page; }); ```
– Игорь Демянюк
Oct 2 '17 at 9:45
I use it for prevent load current page (if user load 6 page and click on 6, data will reload.So i subscribe to params in constuctor. ``` this.routeData = this.activatedRoute.data.subscribe((data) => { this.page = data['pagingParams'].page; this.previousPage = data['pagingParams'].page; }); ```
– Игорь Демянюк
Oct 2 '17 at 9:45
add a comment |
I was searching for answers to this same question and nobody seems to post a clear answer.
Here's my solution:
ngbPagination with ngFor in Angular 7
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;
}
}
<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>
This solution also applies to Angular 6. I have not tested it on other versions.
For more information, check the documentation. Unfortunatley... it lacks vital information regarding ngFor iteration.
Another problem I had was how to set the number of pages. I decided to add my complete solution for those having the same problem. I found this answer here. Take note of the identifier #numPages above.
add a comment |
I was searching for answers to this same question and nobody seems to post a clear answer.
Here's my solution:
ngbPagination with ngFor in Angular 7
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;
}
}
<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>
This solution also applies to Angular 6. I have not tested it on other versions.
For more information, check the documentation. Unfortunatley... it lacks vital information regarding ngFor iteration.
Another problem I had was how to set the number of pages. I decided to add my complete solution for those having the same problem. I found this answer here. Take note of the identifier #numPages above.
add a comment |
I was searching for answers to this same question and nobody seems to post a clear answer.
Here's my solution:
ngbPagination with ngFor in Angular 7
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;
}
}
<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>
This solution also applies to Angular 6. I have not tested it on other versions.
For more information, check the documentation. Unfortunatley... it lacks vital information regarding ngFor iteration.
Another problem I had was how to set the number of pages. I decided to add my complete solution for those having the same problem. I found this answer here. Take note of the identifier #numPages above.
I was searching for answers to this same question and nobody seems to post a clear answer.
Here's my solution:
ngbPagination with ngFor in Angular 7
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;
}
}
<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>
This solution also applies to Angular 6. I have not tested it on other versions.
For more information, check the documentation. Unfortunatley... it lacks vital information regarding ngFor iteration.
Another problem I had was how to set the number of pages. I decided to add my complete solution for those having the same problem. I found this answer here. Take note of the identifier #numPages above.
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;
}
}
<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>
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;
}
}
<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>
edited Nov 24 '18 at 23:41
answered Nov 21 '18 at 18:31
omostanomostan
315
315
add a comment |
add a comment |
Ok. I found an awesome solution in github.
Check it out.
https://github.com/michaelbromley/ng2-pagination
add a comment |
Ok. I found an awesome solution in github.
Check it out.
https://github.com/michaelbromley/ng2-pagination
add a comment |
Ok. I found an awesome solution in github.
Check it out.
https://github.com/michaelbromley/ng2-pagination
Ok. I found an awesome solution in github.
Check it out.
https://github.com/michaelbromley/ng2-pagination
answered Dec 20 '16 at 7:40
Jozef PlachyJozef Plachy
35116
35116
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41223400%2fhow-to-implement-bootstrap-4-for-angular-2-ngb-pagination%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown