Javascript Array paginate(pageNumber, itemsPerPage)

Description

Javascript Array paginate(pageNumber, itemsPerPage)


Array.prototype.paginate = function(pageNumber, itemsPerPage){
 pageNumber   = Number(pageNumber)
 itemsPerPage = Number(itemsPerPage)
 pageNumber   = (pageNumber   < 1 || isNaN(pageNumber))   ? 1 : pageNumber
 itemsPerPage = (itemsPerPage < 1 || isNaN(itemsPerPage)) ? 1 : itemsPerPage

 var start     = ((pageNumber - 1) * itemsPerPage)
 var end       = start + itemsPerPage
 var loopCount = 0
 var result    = {
  data: [],//www .j ava  2  s .c o m
  end: false
 }

 for(loopCount = start; loopCount < end; loopCount++){
  result.data.push(this[loopCount])
 }

 if(loopCount == this.length){
  result.end = true
 }

 return result
}



PreviousNext

Related