Skip to content

Instantly share code, notes, and snippets.

@danallison
Created May 23, 2013 19:43
Show Gist options
  • Save danallison/5638864 to your computer and use it in GitHub Desktop.
Save danallison/5638864 to your computer and use it in GitHub Desktop.
A template function for twitter bootstrap pagination.
paginationTemplate = (data) ->
url = data.url
currentPage = +data.currentPage
totalPages = +data.totalPages
lis = []
if totalPages < 7
for i in [1..totalPages]
li = if i is currentPage then "<li class='active'><a>#{i}</a></li>" else "<li><a href='#{url}/page#{i}'>#{i}</a></li>"
lis.push(li)
else if currentPage < 6
for i in [1..5]
li = if i is currentPage then "<li class='active'><a>#{i}</a></li>" else "<li><a href='#{url}/page#{i}'>#{i}</a></li>"
lis.push(li)
lis.push("<li class='disabled'><a>...</a></li><li><a href='#{url}/page#{totalPages}'>#{totalPages}</a></li>")
else if currentPage > totalPages - 5
lis.push("<li><a href='#{url}/page1'>1</a></li><li class='disabled'><a>...</a></li>")
for i in [4..0]
p = totalPages - i
li = if p is currentPage then "<li class='active'><a>#{p}</a></li>" else "<li><a href='#{url}/page#{p}'>#{p}</a></li>"
lis.push(li)
else
lis.push("<li><a href='#{url}/page1'>1</a></li>
<li class='disabled'><a>...</a></li>
<li><a href='#{url}/page#{currentPage - 1}'>#{currentPage - 1}</a></li>
<li class='active'><a>#{page}</a></li>
<li><a href='#{url}/page#{currentPage + 1}'>#{currentPage + 1}</a></li>
<li class='disabled'><a>...</a></li>
<li><a href='#{url}/page#{totalPages}'>#{totalPages}</a></li>")
lis.unshift(if currentPage < 2 then "<li class='disabled'><a>Prev</a></li>" else "<li><a href='#{url}/page#{currentPage - 1}'>Prev</a></li>")
lis.push(if currentPage >= totalPages then "<li class='disabled'><a>Next</a></li>" else "<li><a href='#{url}/page#{currentPage + 1}'>Next</a></li>")
return "<div class='pagination pagination-centered'>
<ul>
#{lis.join('')}
</ul>
</div>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment