Created
April 22, 2017 21:49
-
-
Save culttm/43adfd3b8a51917278e54c211a4c839a to your computer and use it in GitHub Desktop.
pagination script
This file contains hidden or 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
| 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