I'm using a pagination component from ng-bootstrap (which I customized) and I want to reuse the component in other places, without having to rewrite code.
Since the ngb-pagination
component has a two-way binding property page
, I need properly to expose that property to the parent component.
I know that the issue is with the component bellow, because if I use the standard one, not inside a component, it works normally.
import { Component, Input, Output, OnInit, EventEmitter } from '@angular/core';
import { faChevronLeft, faChevronRight, faAngleDoubleLeft, faAngleDoubleRight } from '@fortawesome/free-solid-svg-icons';
@Component({
selector: 'app-pagination',
template: `
<div class="d-flex justify-content-between p-2">
<ngb-pagination [collectionSize]="collectionSize" [page]="page" (pageChange)="pageChange" [pageSize]="pageSize" [boundaryLinks]="true">
<ng-template ngbPaginationFirst>
<fa-icon class="fas" [icon]="faAngleDoubleLeft" aria-hidden="true"></fa-icon>
</ng-template>
<ng-template ngbPaginationLast>
<fa-icon class="fas" [icon]="faAngleDoubleRight" aria-hidden="true"></fa-icon>
</ng-template>
<ng-template ngbPaginationPages let-page let-pages="pages">
<li class="ngb-custom-pages-item" *ngIf="pages.length > 0">
<div class="form-group d-flex flex-nowrap px-2">
<label id="paginationInputLabel" for="paginationInput" class="col-form-label mr-2 ml-1">Page</label>
<input #i type="text" inputmode="numeric" pattern="[0-9]*" class="form-control custom-pages-input"
id="paginationInput" [value]="page" (keyup.enter)="selectPage(i.value)" (blur)="selectPage(i.value)"
(input)="formatInput($any($event).target)" aria-labelledby="paginationInputLabel paginationDescription"
style="width: 2.5rem"/>
<span id="paginationDescription" class="col-form-label text-nowrap px-2">of {{pages.length}}</span>
</div>
</li>
</ng-template>
<ng-template ngbPaginationPrevious>
<fa-icon class="fas" [icon]="faChevronLeft" aria-hidden="true"></fa-icon>
</ng-template>
<ng-template ngbPaginationNext>
<fa-icon class="fas" [icon]="faChevronRight" aria-hidden="true"></fa-icon>
</ng-template>
</ngb-pagination>
</div>
`,
styleUrls: ['./pagination.component.scss']
})
export class PaginationComponent implements OnInit {
faChevronLeft = faChevronLeft;
faChevronRight = faChevronRight;
faAngleDoubleLeft = faAngleDoubleLeft;
faAngleDoubleRight = faAngleDoubleRight;
@Input() collectionSize: number = 20;
@Input() page: number = 1;
@Output() pageChange = new EventEmitter<number>();
@Input() pageSize: number = 10;
constructor() { }
ngOnInit(): void {
}
selectPage(page: string) {
this.page = parseInt(page, 10) || 1;
}
formatInput(input: HTMLInputElement) {
input.value = input.value.replace('/[^0-9]/g', '');
}
}
And I simple use the component as this (I'm following the complete table example):
<app-pagination [collectionSize]="(total$ | async)!" [(page)]="service.page" [pageSize]="service.pageSize">
</app-pagination>
So, how can I properly expose the properties and events?
CodePudding user response:
You need to emit events from component:
<ngb-pagination [collectionSize]="collectionSize" [page]="page" (pageChange)="pageChange.emit($event)" [pageSize]="pageSize" [boundaryLinks]="true">
...
selectPage(page: string) {
this.page = parseInt(page, 10) || 1;
this.pageChange.emit(this.page);
}