Skip to content

Instantly share code, notes, and snippets.

@harveyxia
Created July 21, 2014 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harveyxia/a4424cdd86a98cf42886 to your computer and use it in GitHub Desktop.
Save harveyxia/a4424cdd86a98cf42886 to your computer and use it in GitHub Desktop.
Pagination in ejs
<!-- provide these args: currentPage, totalPages, and queryParams -->
<!-- variables i and j control the number of pages to show on the left and right -->
<div class='pagination-container col-xs-12'>
<ul class='pagination pagination-lg'>
<% if (currentPage != 1) { %>
<li><a class='ajax' href="/<%= paginateItem %>/?page=<%= currentPage - 1 %><%= queryParams%>">&laquo;</a></li>
<% } else { %>
<li class='disabled'><a class='ajax' href="javascript:void(0)">&laquo;</a></li>
<% } %>
<% var i = 5, j = 5; %>
<% i += Math.max(0, currentPage + j - totalPages) %>
<% j -= Math.max(0, currentPage + j - totalPages) %>
<% for (var i; i > 0; i--) { %>
<% if (currentPage - i > 0) { %>
<li><a class='ajax' href="/<%= paginateItem %>?page=<%=currentPage - i%><%= queryParams%>"><%= currentPage - i %></a></li>
<% } else { %>
<% j++ %>
<% } %>
<% } %>
<li class='active ajax'><a href="javascript:void()"><%= currentPage %></a></li>
<% for (var a = 1; a <= j && currentPage + a <= totalPages; a++) { %>
<li><a class='ajax' href="/<%= paginateItem %>?page=<%=currentPage + a%><%= queryParams%>"><%= currentPage + a %></a></li>
<% } %>
<% if (currentPage != totalPages) { %>
<li><a class='ajax' href="/<%= paginateItem %>/?page=<%= currentPage + 1 %><%= queryParams%>">&raquo;</a></li>
<% } else { %>
<li class='disabled'><a class='ajax' href="javascript:void(0)">&raquo;</a></li>
<% } %>
</ul>
</div>
@Globik
Copy link

Globik commented Jul 11, 2015

What's this? Is it: << 1 2 3 4 5 ...10 11 12 13 14 15>> ?

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