Created
January 31, 2018 21:18
-
-
Save gdugas/b61d79b5c0e9b6827ae29a3926791da1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface Paginator { | |
getPageDatas(page: number): Promise<any[]>; | |
getNbPages(): Promise<number>; | |
getNbRowsPerPage(): Promise<number>; | |
} | |
import { Paginator } from "./paginator.interface"; | |
export class StaticPaginator implements Paginator { | |
private datas: any[]; | |
private rowsPerPage = 10; | |
constructor(datas: any[]) { | |
this.datas = datas; | |
} | |
getPageDatas(page: number): any[] { | |
if (page <= 0 || page > this.getNbPages()) { | |
return []; | |
} | |
const firstIndex = (page - 1) * this.getNbRowsPerPage(); | |
const lastIndex = firstIndex + this.getNbRowsPerPage(); | |
return this.datas.slice(firstIndex, lastIndex); | |
} | |
getNbPages(): number { | |
return Math.ceil(this.datas.length / this.getNbRowsPerPage()); | |
} | |
getNbRowsPerPage(): number { | |
return this.rowsPerPage; | |
} | |
setNbRowsPerPage(nbRows: number): void { | |
if (nbRows <= 0) { | |
throw "invalid rows number: nb rows per page must be positive"; | |
} | |
this.rowsPerPage = nbRows; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment