Skip to content

Instantly share code, notes, and snippets.

@byronmansfield
Last active August 29, 2015 14:23
Show Gist options
  • Save byronmansfield/d4ea5aef83cdf35d3fbc to your computer and use it in GitHub Desktop.
Save byronmansfield/d4ea5aef83cdf35d3fbc to your computer and use it in GitHub Desktop.
Angular Pagination Factory
'use strict';
/**
* @ngdoc factory
* @name perfmanApp.Paginator
* @description
* # Paginator
* Factory in the perfmanApp.
*/
angular.module('perfmanApp')
.factory('Paginator'
, function Paginator() {
return function(data, options) {
/**
* Globals
*/
options = options || {}
var pageSize = options.pageSize || 5
var currentPage = options.currentPage || 1
this.data = data
/**
* Total Page Count
* @return {Number} - Total number of pages
*/
this.totalPages = function() {
var pages = Math.floor(data.length/pageSize)
if(pages === 0)
pages = 1
return pages
}
/**
* Go To specific page number
* @params {Number} - Page number to go to
*/
this.goTo = function(pageNumber) {
currentPage = pageNumber
this.data = pagedData()
}
/**
* Current Page
* @return {Number} - the current page viewed
*/
this.currentPage = function() {
return currentPage
}
/**
* Paged Data
* @description - The Finial Array list of items for the currently viewed page
* @return {Array} - List of items for currently viewed page
*/
var pagedData = function() {
var start = (currentPage - 1) * pageSize
var end = start + pageSize
return data.slice(start, end)
}
/**
* Return the list of items for the currently viewed page
*/
this.data = pagedData()
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment