pagination.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. function Pagination(paginatedList, paginationContainer, paginationLimit = 5, listItemTag = "li") {
  2. const createMainPagination = () => {
  3. paginationContainer.innerHTML =
  4. `
  5. <nav aria-label="Page navigation example">
  6. <ul class="pagination justify-content-center">
  7. <li class="page-item disabled" id="prev-button" tabindex="-1">
  8. <a class="page-link" href="${location.hash}" tabindex="-1">Previous</a>
  9. </li>
  10. <!--<li class="page-item"><a class="page-link" href="${location.hash}">1</a></li>
  11. <li class="page-item"><a class="page-link" href="${location.hash}">2</a></li>
  12. <li class="page-item"><a class="page-link" href="${location.hash}">3</a></li>-->
  13. <li class="page-item" id="next-button">
  14. <a class="page-link" href="${location.hash}">Next</a>
  15. </li>
  16. </ul>
  17. </nav>
  18. `;
  19. }
  20. createMainPagination();
  21. const listItems = paginatedList.querySelectorAll(listItemTag);
  22. const nextButton = paginationContainer.querySelectorAll("#next-button")[0];
  23. const prevButton = paginationContainer.querySelectorAll("#prev-button")[0];
  24. const pageCount = Math.ceil(listItems.length / paginationLimit);
  25. let currentPage = 1;
  26. const disableButton = (button) => {
  27. button.classList.add("disabled");
  28. button.setAttribute("disabled", true);
  29. };
  30. const enableButton = (button) => {
  31. button.classList.remove("disabled");
  32. button.removeAttribute("disabled");
  33. };
  34. const handlePageButtonsStatus = () => {
  35. if (currentPage === 1) {
  36. disableButton(prevButton);
  37. } else {
  38. enableButton(prevButton);
  39. }
  40. if (pageCount === currentPage) {
  41. disableButton(nextButton);
  42. } else {
  43. enableButton(nextButton);
  44. }
  45. };
  46. const handleActivePageNumber = () => {
  47. document.querySelectorAll(".page-item").forEach((button) => {
  48. button.classList.remove("active");
  49. const pageIndex = Number(button.getAttribute("page-index"));
  50. if (pageIndex == currentPage) {
  51. button.classList.add("active");
  52. }
  53. });
  54. };
  55. const appendPageNumber = (index) => {
  56. const pageNumber = document.createElement("li");
  57. pageNumber.classList.add("page-item");
  58. pageNumber.innerHTML = `<a class="page-link" href="#${location.hash}">${index}</a>`;
  59. pageNumber.setAttribute("page-index", index);
  60. pageNumber.setAttribute("aria-label", "Page " + index);
  61. nextButton.parentNode.insertBefore(pageNumber, nextButton);
  62. };
  63. const getPaginationNumbers = () => {
  64. for (let i = 1; i <= pageCount; i++) {
  65. appendPageNumber(i);
  66. }
  67. };
  68. const setCurrentPage = (pageNum) => {
  69. currentPage = pageNum;
  70. if (currentPage < 1 || currentPage > pageCount)
  71. return;
  72. handleActivePageNumber();
  73. handlePageButtonsStatus();
  74. const prevRange = (pageNum - 1) * paginationLimit;
  75. const currRange = pageNum * paginationLimit;
  76. listItems.forEach((item, index) => {
  77. item.classList.add("d-none");
  78. if (index >= prevRange && index < currRange) {
  79. item.classList.remove("d-none");
  80. }
  81. });
  82. };
  83. function init() {
  84. getPaginationNumbers();
  85. setCurrentPage(1);
  86. prevButton.addEventListener("click", (event) => {
  87. event.preventDefault();
  88. setCurrentPage(currentPage - 1);
  89. });
  90. nextButton.addEventListener("click", (event) => {
  91. event.preventDefault();
  92. setCurrentPage(currentPage + 1);
  93. });
  94. document.querySelectorAll(".page-item").forEach((button) => {
  95. const pageIndex = Number(button.getAttribute("page-index"));
  96. if (pageIndex) {
  97. button.childNodes[0].addEventListener("click", (event) => {
  98. event.preventDefault();
  99. if (pageIndex && !button.classList.contains('disabled'))
  100. setCurrentPage(pageIndex);
  101. });
  102. }
  103. });
  104. }
  105. init();
  106. }