Skip to content

Instantly share code, notes, and snippets.

@romanzaycev
Last active May 14, 2020 07:38
Show Gist options
  • Save romanzaycev/0fd07414cd59ba0a5a4cf60b95fe9f18 to your computer and use it in GitHub Desktop.
Save romanzaycev/0fd07414cd59ba0a5a4cf60b95fe9f18 to your computer and use it in GitHub Desktop.
Jade (Pug) Bootstrap pagination mixin (Digg-like pagination)
mixin pagination(adjacents, numPages, display, currentPage, base)
- adjacents = (adjacents || 1) * 1;
- numPages = (numPages || 10) * 1;
- currentPage = (currentPage || 1) * 1;
- base = base || '#';
- display = (display || 7) * 1;
ul.pagination
if numPages < display + adjacents * 2
- var p = 1;
while p <= numPages
if currentPage == p
li.active
a(href=base + p)
| #{p}
else
li.page
a(href=base + p)
| #{p}
- p++;
else if numPages >= 7 + adjacents * 2
if currentPage < 1 + adjacents * 3
- var p = 1;
while p < display - 3 + adjacents * 2
if currentPage == p
li.active
a(href=base + p)
| #{p}
else
li.page
a(href=base + p)
| #{p}
- p++;
li.disabled
a(href="#")
| &hellip;
li.page
a(href=base + (numPages - 1))
| #{numPages - 1}
li.page
a(href=base + numPages)
| #{numPages}
else if numPages - adjacents * 2 > currentPage && currentPage > adjacents * 2
li.page
a(href=base + 1)
| 1
li.page
a(href=base + 2)
| 2
li.disabled
a(href="#")
| &hellip;
- var p = currentPage - adjacents;
while p <= currentPage + adjacents
if currentPage == p
li.active
a(href=base + p)
| #{p}
else
li.page
a(href=base + p)
| #{p}
- p++;
li.disabled
a(href="#")
| &hellip;
li.page
a(href=base + (numPages - 1))
| #{numPages - 1}
li.page
a(href=base + numPages)
| #{numPages}
else
li.page
a(href=base + 1)
| 1
li.page
a(href=base + 2)
| 2
li.disabled
a(href="#")
| &hellip;
- var p = numPages - (1 + (adjacents * 3));
while p <= numPages
if currentPage == p
li.active
a(href=base + p)
| #{p}
else
li.page
a(href=base + p)
| #{p}
- p++;
@mdimai666
Copy link

Use it please:
- adjacents = (adjacents || 3)*1;
- numPages = (numPages || 10)*1;
- currentPage = (currentPage || 1)*1;
- base = base || '#';
- display = (display || 7) *1;

this will help to avoid string problems: 27 + 1 = 271

@romanzaycev
Copy link
Author

@mdimai666

Use it please:
- adjacents = (adjacents || 3)*1;
...

Thanks!

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