projects/rebirth-ng/src/lib/pager/pager.component.ts
changeDetection | ChangeDetectionStrategy.OnPush |
exportAs | pager |
selector | re-pager |
styleUrls | pager.component.scss |
templateUrl | ./re-pager.component.html |
Properties |
|
Methods |
Inputs |
Outputs |
constructor(rebirthNGConfig: RebirthNGConfig)
|
||||||
Parameters :
|
aligned
|
Type: |
cssClass
|
Type: |
disable
|
Default value: |
nextText
|
Type: |
pageIndex
|
Default value: |
pageSize
|
Type: |
previousText
|
Type: |
total
|
Default value: |
pageIndexChange
|
$event type: EventEmitter
|
Private getTotalPage |
getTotalPage()
|
Returns :
any
|
hasNext |
hasNext()
|
Returns :
boolean
|
hasPrev |
hasPrev()
|
Returns :
boolean
|
next |
next()
|
Returns :
void
|
ngOnChanges | ||||||
ngOnChanges(changes: SimpleChanges)
|
||||||
Parameters :
Returns :
void
|
onPageIndexChange | ||||||
onPageIndexChange(pageIndex: number)
|
||||||
Parameters :
Returns :
void
|
prev |
prev()
|
Returns :
void
|
Static EFFECT_PAGE_RANGE_KEYS |
EFFECT_PAGE_RANGE_KEYS:
|
Type : []
|
Default value : ['total', 'pageSize', 'pageIndex']
|
totalPage |
totalPage:
|
Type : number
|
Default value : 0
|
import {
Component, ChangeDetectionStrategy, Input, Output, EventEmitter, OnChanges,
SimpleChanges
} from '@angular/core';
import { RebirthNGConfig } from '../rebirth-ng.config';
@Component({
selector: 're-pager',
templateUrl: './re-pager.component.html',
styleUrls: ['./pager.component.scss'],
exportAs: 'pager',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class PagerComponent implements OnChanges {
static EFFECT_PAGE_RANGE_KEYS = ['total', 'pageSize', 'pageIndex'];
@Input() total = 0;
@Input() pageSize: number;
@Input() pageIndex = 1;
@Output() pageIndexChange = new EventEmitter<number>();
@Input() aligned: boolean;
@Input() previousText: string;
@Input() nextText: string;
@Input() disable = false;
@Input() cssClass: string;
totalPage = 0;
constructor(private rebirthNGConfig: RebirthNGConfig) {
this.aligned = rebirthNGConfig.pager.aligned;
this.pageSize = rebirthNGConfig.pager.pageSize;
this.previousText = rebirthNGConfig.pager.button.previous;
this.nextText = rebirthNGConfig.pager.button.next;
}
prev(): void {
if (this.hasPrev()) {
this.onPageIndexChange(this.pageIndex - 1);
}
}
next(): void {
if (this.hasNext()) {
this.onPageIndexChange(this.pageIndex + 1);
}
}
onPageIndexChange(pageIndex: number) {
if (this.pageIndex !== pageIndex) {
this.pageIndexChange.emit(pageIndex);
}
}
hasPrev(): boolean {
return this.pageIndex > 1;
}
hasNext(): boolean {
return this.pageIndex < this.totalPage;
}
private getTotalPage() {
return Math.ceil(this.total / this.pageSize);
}
ngOnChanges(changes: SimpleChanges): void {
const shouldUpdateRanges = PagerComponent.EFFECT_PAGE_RANGE_KEYS.some(key => !!changes[key]);
if (shouldUpdateRanges) {
this.totalPage = this.getTotalPage();
this.pageIndex = Math.max(Math.min(this.pageIndex, this.totalPage), 1);
}
}
}
<nav aria-label="pager" class="{{cssClass}}">
<ul class="pager">
<li [ngClass]="{'previous': aligned, 'disabled': disable && !hasPrev()}" *ngIf="disable || hasPrev()">
<a (click)="prev()">{{ previousText}}</a></li>
<li [ngClass]="{'next': aligned, 'disabled': disable && !hasNext()}" *ngIf="disable || hasNext()">
<a (click)="next()">{{ nextText}}</a></li>
</ul>
</nav>