Pagination with arrows in javascript -
here javascript code:
function setpage(currentpage){ currentpage = parseint(currentpage) + 1; var last = json.parse(localstorage.getitem("attempt")); var lastpage = last.length - 1; var prevcurpage = currentpage - 1; var prevcurpage2 = currentpage - 2; var nextcurpage = currentpage + 1; var nextcurpage2 = currentpage + 2; var content = ""; if(lastpage == 1){ }else{ if(currentpage == 1){ if(lastpage > 2){ content+=" [<a href=''>"+currentpage+"</a>] | "; content+=" <a href=''>"+nextcurpage+"</a> | "; content+=" <a href=''>"+nextcurpage2+"</a> | "; }else{ content+=" [<a href=''>"+currentpage+"</a>] | "; content+=" <a href=''>"+nextcurpage+"</a> | "; } }else if(currentpage == lastpage){ if(lastpage > 2){ content+=" <a href=''>"+prevcurpage2+"</a> | "; content+=" <a href=''>"+prevcurpage+"</a> | "; content+=" [<a href=''>"+currentpage+"</a>] | "; }else{ content+=" <a href=''>"+prevcurpage+"</a> | "; content+=" [<a href=''>"+currentpage+"</a>] | "; } }else{ content+=" <a href=''>"+prevcurpage+"</a> | "; content+=" [<a href=''>"+currentpage+"</a>] | "; content+=" <a href=''>"+nextcurpage+"</a> | "; } }
the currentpage started in 0
; need add 1
; why not displaying correct pagination? , how put arrows?
i want show,
currentpage = 0; [1] | 2 | 3 > currentpage = 2; 1 | 2 | [3] | 4 | 5 > currentpage = 7; < 4 | 5 | [6]
try this:
if (currentpage > 3) content += " < "; content += currentpage - 2 > 0 ? "<a href=''>"+(currentpage - 2)+"</a> | " : ''; content += currentpage - 1 > 0 ? "<a href=''>"+(currentpage - 1)+"</a> | " : ''; content += "[" + currentpage + "]"; content += currentpage + 1 <= lastpage ? " | <a href=''>"+(currentpage + 1)+"</a>" : ''; content += currentpage + 2 <= lastpage ? " | <a href=''>"+(currentpage + 2)+"</a>" : ''; if (currentpage < lastpage - 2) content += " > ";
or better flexibility, use sort of range variable:
var range = 2; if (currentpage > range + 1) content += " < "; (var = range; > 0; a--) content += currentpage - > 0 ? "<a href=''>"+(currentpage - a)+"</a> | " : ''; content += "[" + currentpage + "]"; (var = 1; <= range; a++) content += currentpage + <= lastpage ? " | <a href=''>"+(currentpage + a)+"</a>" : ''; if (currentpage < lastpage - range) content += " > ";