Skip to content

Instantly share code, notes, and snippets.

@thevtm
Forked from backus/pagination.jade
Created September 15, 2016 19:10
Show Gist options
  • Save thevtm/5e3a0f2fa95e69e8c7cd308599355249 to your computer and use it in GitHub Desktop.
Save thevtm/5e3a0f2fa95e69e8c7cd308599355249 to your computer and use it in GitHub Desktop.
Jade + Bootstrap Pagination Mixin
//- Pagination mixin
//- ----------------
//- numPages = # links to serve up
//- numButtons = # of "side" buttons that will be present.
//- 2 numButtons will result in | << | 1 | 2 |>3<| 4 | 5 | >> |
//- curr = current page
//- base = base url before num
//- start at page 1
//-
//- Example:
//- pagination(10, 3, 4, '/fda/')
//- Yields: | « | 1 | 2 | 3 |>4<| 5 | 6 | 7 | » |
//- « = /fda/2, 3 = /fda/3, 4 = #, .. 7 = /fda/7, » = /fda/8
mixin pagination(numPages, numButtons, curr, base)
- let start = 1
- numPages = numPages || 10
- numButtons = numButtons || 4
- curr = curr || 1
- base = base || '#'
- let highestFirstPage = numPages - (numButtons * 2) - 1
- let smallestLastPage = start + (numButtons * 2)
- let firstButton = Math.min(highestFirstPage, Math.max(start, (curr - numButtons)))
- let lastButton = Math.max(smallestLastPage, Math.min(numPages, (curr + numButtons)))
ul.pagination
- if(curr === start)
li.disabled
a Prev
- else
li
a(href=base + (curr - 1)) Prev
- for(let i = firstButton; i <= lastButton; i++) {
- if(curr === i)
li.active
a(href=base + i) #{i}
- else
li
a(href=base + i) #{i}
- }
- if(curr === numPages)
li.disabled
a Next
- else
li
a(href=base + (curr + 1)) Next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment