@neoprospecta/angular-data-box
Version:
Data table with REST implementation.
48 lines (38 loc) • 1.8 kB
text/typescript
import { Component, Output, Input, EventEmitter, DoCheck } from '@angular/core';
export class Paginator implements DoCheck {
totalItems: number;
currentPage: number;
amountPerPage: number;
pageChanged = new EventEmitter<any>();
totalOfPagesRoundedUp: number;
totalOfPagesRoundedDown: number;
constructor() { }
ngDoCheck() {
this.totalOfPagesRoundedUp = Math.ceil(this.totalItems / this.amountPerPage); // arredonda pra cima
this.totalOfPagesRoundedDown = Math.floor(this.totalItems / this.amountPerPage); // arredonda pra baixo
}
emitPageChange(direction: string) {
this.pageChanged.emit({ event: direction })
}
getCurrentPages() {
if (this.totalItems === 0){
return '0';
}
return this.getFirstPageIndex() + ' - ' + this.getLastPageIndex();
}
getFirstPageIndex() {
return ((this.currentPage - 1) * this.amountPerPage) + 1;
}
getLastPageIndex() {
if ((this.currentPage === this.totalOfPagesRoundedUp) && (this.totalOfPagesRoundedDown < this.totalOfPagesRoundedUp)) { // last page and not full items
return this.totalItems;
} else {
return (this.currentPage * this.amountPerPage);
}
}
}