Skip to content

Instantly share code, notes, and snippets.

@ChrisMcKee
Created May 17, 2011 11:57
Show Gist options
  • Select an option

  • Save ChrisMcKee/976343 to your computer and use it in GitHub Desktop.

Select an option

Save ChrisMcKee/976343 to your computer and use it in GitHub Desktop.
Pagination
function PagnationStr(totalPgs, currentPg)
{
var pageSet = 10,
indexUrl = "//mydomain.com/page/",
tmp = "<div class=\"pagination\"><ul>",
maxpage = currentPg + 9,
crtarget = currentPg,
i = 0;
if (currentPg < 2 && currentPg > 0)
{
indexUrl = indexUrl + "/";
}
// Previous Link
if (currentPg > 1)
{
tmp += ("<li class='previous'><a href='" + indexUrl + (currentPg - 1) +
"' title=\"Move One Page Back\">&laquo; Previous</a></li>");
}
else
{
tmp += ("<li class='previous-off'>&laquo; Previous</li>");
}
// Pagination Loop
if (maxpage > totalPgs && maxpage > 10){ crtarget = crtarget - 9;}
for (i = crtarget; i < maxpage; i++)
{
var cssclass = (currentPg === i) ? " class=\"currentpage\" " : "",
pno = i > 1 ? i.toString() : "";
if (i < 1){ i = 1; }
if (i > totalPgs){ break; }
tmp += ("<li><a href='" + indexUrl + pno + "'" + cssclass + ">" + i + "</a></li>");
if (pageSet < 1){ break; }
pageSet--;
}
// NEXT LINK
tmp += ((totalPgs > 1) && (currentPg + 1 < totalPgs)) ?
("<li class='next'><a href='" + indexUrl + (currentPg + 1) +
"' title=\"Move One Page Forward\">Next &raquo;</a></li>") :
("<li class='next-off'>Next &raquo;</li>");
return tmp += ("</ul></div>");
}
@ChrisMcKee
Copy link
Author

Lint Test

@ChrisMcKee
Copy link
Author

Simple Javascript pagination function, returns html string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment