-
-
Save edinsoncs/45fb15dd3d6e3bba06c3da810aeaab91 to your computer and use it in GitHub Desktop.
javascript pagination algorithm
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
var pagenation = function(current, total){ | |
var list = []; | |
var pageLimit = 5; | |
var upperLimit, lowerLimit; | |
var currentPage = lowerLimit = upperLimit = Math.min(current, total); | |
for (var b = 1; b < pageLimit && b < total;) { | |
if (lowerLimit > 1 ) { | |
lowerLimit--; b++; | |
} | |
if (b < pageLimit && upperLimit < total) { | |
upperLimit++; b++; | |
} | |
} | |
for (var i = lowerLimit; i <= upperLimit; i++) { | |
if (i == currentPage){ | |
list.push("["+i+"]"); | |
} | |
else{ | |
list.push(i); | |
} | |
} | |
console.log(list); | |
return list; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment