Last active
March 24, 2021 06:16
-
-
Save keon/5380f81393ad98ec19e6 to your computer and use it in GitHub Desktop.
javascript pagination algorithm
This file contains 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
Awesome code. Just found small one bug.
it is not working with
var pageLimit = 2;
for page 1 it is giving
Selected page 1: (2) ["[1]", 2]
and also for page 2 it is giving the sameSelected page 2: (2) [1, "[2]"]
because of this user cannot go to the third page.