Skip to content

Instantly share code, notes, and snippets.

@culttm
Created April 22, 2017 21:49
Show Gist options
  • Select an option

  • Save culttm/43adfd3b8a51917278e54c211a4c839a to your computer and use it in GitHub Desktop.

Select an option

Save culttm/43adfd3b8a51917278e54c211a4c839a to your computer and use it in GitHub Desktop.
pagination script
module.exports = function (current, length, displayLength) {
if (length <= 1) return []
displayLength = displayLength - 2
var indexes = [1]
var start = Math.round(current - displayLength / 2)
var end = Math.round(current + displayLength / 2)
if (start <= 1) {
start = 2
end = start + displayLength - 1
if (end >= length - 1) end = length - 1
}
if (end >= length - 1) {
end = length - 1
start = end - displayLength + 1
if (start <= 1) start = 2
}
if (start !== 2) indexes.push('...')
for (var i = start; i <= end; i++) {
indexes.push(i)
}
if (end !== length - 1) indexes.push('...')
indexes.push(length)
return indexes
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment