pagination.ejs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <!-- 动态分页控制 -->
  2. <div class="pagination">
  3. <!-- 上一页按钮 -->
  4. <a href="<%= pageUri(uri, page - 1 == 0 ? 1 : page - 1)%>" class="page-btn" <%= page === 1 ? 'style="pointer-events: none; opacity: 0.5;"' : '' %>>
  5. <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
  6. <path d="M15 18l-6-6 6-6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
  7. </svg>
  8. </a>
  9. <!-- 分页数字按钮 -->
  10. <%
  11. // 计算总页数
  12. const totalPages = Math.ceil(recordsFiltered / length);
  13. // 设置显示的页码范围(当前页前后各2页)
  14. let startPage = Math.max(1, page - 2);
  15. let endPage = Math.min(totalPages, startPage + 4);
  16. // 调整起始页以确保显示5个页码
  17. if (endPage - startPage < 4) {
  18. startPage = Math.max(1, endPage - 4);
  19. }
  20. // 显示第一页和省略号(如果需要)
  21. if (startPage > 1) { %>
  22. <a href="<%= pageUri(uri, 1)%>" class="page-btn"><%= 1 %></a>
  23. <% if (startPage > 2) { %>
  24. <span class="page-btn" style="background: none; cursor: default;">...</span>
  25. <% } %>
  26. <% }
  27. // 显示页码范围
  28. for (let i = startPage; i <= endPage; i++) { %>
  29. <a href="<%= pageUri(uri, i)%>" class="page-btn <%= i === page ? 'active' : '' %>"><%= i %></a>
  30. <% }
  31. // 显示最后一页和省略号(如果需要)
  32. if (endPage < totalPages) {
  33. if (endPage < totalPages - 1) { %>
  34. <span class="page-btn" style="background: none; cursor: default;">...</span>
  35. <% } %>
  36. <a href="<%= pageUri(uri, totalPages)%>" class="page-btn"><%= totalPages %></a>
  37. <% } %>
  38. <!-- 下一页按钮 -->
  39. <a href="<%= pageUri(uri, page + 1 > totalPages ? totalPages : page + 1)%>" class="page-btn" <%= page === totalPages ? 'style="pointer-events: none; opacity: 0.5;"' : '' %>>
  40. <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
  41. <path d="M9 18l6-6-6-6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
  42. </svg>
  43. </a>
  44. </div>