import { IPaginationService } from "../interfaces/IPaginationService" ; import { IRow } from "../interfaces/IRow" ; import * as _ from "lodash" ; import { Injectable } from "@angular/core" ; @ Injectable ( ) export class ArrayPaginationService implements IPaginationService { itemsPerPage : number ; page : number ; totalRowsCount : number ; constructor ( ) { this . page = this . page || 1 ; } transformRows ( rows ) { if ( this . isActive ( ) == false ) { return rows ; } this . totalRowsCount = rows . length ; return _ . slice ( rows , this . getStartIndex ( ) , this . getStartIndex ( ) + this . itemsPerPage ) ; } isActive ( ) { return this . itemsPerPage > 0 ; } setItemsPerPage ( pageSize ) { this . itemsPerPage = pageSize ; this . page = 1 ; } getStartIndex ( ) { return ( this . page - 1 ) * this . itemsPerPage ; } nextPage ( ) { if ( this . hasNextPage ( ) ) { this . page ++ ; } } previousPage ( ) { if ( this . hasPreviousPage ( ) ) { this . page -- ; } } hasPreviousPage ( ) { return this . page > 1 ; } hasNextPage ( ) { var totalPages = Math . ceil ( this . totalRowsCount / this . itemsPerPage ) ; return this . page < totalPages ; } }