Advertisement
Grossos

pagination-fixed

May 5th, 2024
828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const count = await axios('http://localhost:5000/sitecount');
  2.  
  3. const pageNumbers = (total, max, current) => {
  4.     const half = Math.round(max / 2);
  5.     let to = max;
  6.  
  7.     if (current + half >= total) {
  8.         to = total;
  9.     } else if (current > half) {
  10.         to = current + half
  11.     }
  12.  
  13.     let from = to - max;
  14.  
  15.     return Array.from({ length: max }, (_, i) => (i + 1) + from);
  16. };
  17.  
  18. function PaginationButtons(totalPages, maxPageVisible = 10, initialPage = 1) {
  19.     let pages = pageNumbers(totalPages, maxPageVisible, initialPage);
  20.     let currentPageBtn = null;
  21.     const buttons = new Map();
  22.     const fragment = document.createDocumentFragment();
  23.     const paginationButtonsContainer = document.createElement('div');
  24.     paginationButtonsContainer.className = 'container-page';
  25.  
  26.     const updateActivePage = newPageNumber => {
  27.         currentPage = newPageNumber;
  28.         sessionStorage.setItem('activePage', currentPage);
  29.         pages = pageNumbers(totalPages, maxPageVisible, currentPage);
  30.     }
  31.  
  32.     let currentPage = parseInt(sessionStorage.getItem('activePage')) || initialPage;
  33.  
  34.     updateActivePage(currentPage);
  35.  
  36.     const urlParams = new URLSearchParams(window.location.search);
  37.     const pageParam = urlParams.get('page');
  38.     if (!pageParam) {
  39.         updateActivePage(1);
  40.     }
  41.  
  42.     const disabled = {
  43.         start: () => currentPage === 1 && pages[0] === 1,
  44.         prev: () => currentPage === 1,
  45.         next: () => currentPage === totalPages,
  46.         end: () => pages.slice(-1)[0] === totalPages && currentPage === totalPages
  47.     }
  48.  
  49.     const createAndSetupButton = (label = '', cls = '', disabled = false, handleClick = () => { }) => {
  50.         const button = document.createElement('button');
  51.         button.textContent = label;
  52.         button.className = `page-btn ${cls}`;
  53.         button.disabled = disabled;
  54.         button.addEventListener('click', event => {
  55.             handleClick(event);
  56.             this.update();
  57.             paginationButtonsContainer.value = currentPage;
  58.             paginationButtonsContainer.dispatchEvent(new Event('change'));
  59.         })
  60.  
  61.         return button;
  62.     }
  63.  
  64.     const onPageButtonClick = e => currentPage = Number(e.currentTarget.textContent);
  65.  
  66.     const onPageButtonUpdate = index => btn => {
  67.         btn.textContent = pages[index];
  68.  
  69.         if (pages[index] === currentPage) {
  70.             currentPageBtn.classList.remove('active');
  71.             btn.classList.add('active');
  72.             currentPageBtn = btn;
  73.             currentPageBtn.focus();
  74.         }
  75.     }
  76.  
  77.     buttons.set(
  78.         createAndSetupButton('start', 'start-page', disabled.start(), () => currentPage = 1),
  79.         (btn) => btn.disabled = disabled.start()
  80.     )
  81.  
  82.     buttons.set(
  83.         createAndSetupButton('prev', 'prev-page', disabled.prev(), () => currentPage -= 1),
  84.         (btn) => btn.disabled = disabled.prev()
  85.     )
  86.  
  87.     pages.forEach((pageNumber, index) => {
  88.         const isCurrentPage = pageNumber === currentPage;
  89.         const button = createAndSetupButton(pageNumber, isCurrentPage ? 'active' : '', false, onPageButtonClick);
  90.  
  91.         if (isCurrentPage) {
  92.             currentPageBtn = button;
  93.         }
  94.  
  95.         buttons.set(button, onPageButtonUpdate(index))
  96.     })
  97.  
  98.     buttons.set(
  99.         createAndSetupButton('next', 'next-page', disabled.next(), () => currentPage += 1),
  100.         (btn) => btn.disabled = disabled.next()
  101.     )
  102.  
  103.     buttons.set(
  104.         createAndSetupButton('end', 'end-page', disabled.end(), () => currentPage = totalPages),
  105.         (btn) => btn.disabled = disabled.end()
  106.     )
  107.  
  108.     buttons.forEach((_, btn) => fragment.appendChild(btn));
  109.  
  110.     this.render = (container = document.querySelector('.card-body')) => {
  111.  
  112.         paginationButtonsContainer.appendChild(fragment);
  113.         container.appendChild(paginationButtonsContainer);
  114.     }
  115.  
  116.     this.update = (newPageNumber = currentPage) => {
  117.  
  118.         updateActivePage(newPageNumber);
  119.         sessionStorage.setItem('currentPage', currentPage);
  120.  
  121.         currentPage = newPageNumber;
  122.         pages = pageNumbers(totalPages, maxPageVisible, currentPage);
  123.         buttons.forEach((updateButton, button) => updateButton(button));
  124.     }
  125.  
  126.     this.onChange = (handler) => {
  127.         paginationButtonsContainer.addEventListener('change', handler)
  128.     }
  129. };
  130.  
  131. const paginationButtons = new PaginationButtons(Math.ceil(count.data / 50));
  132.  
  133. paginationButtons.render();
  134. paginationButtons.onChange(e => {
  135.     let page = e.target.value;
  136.     let currentUrl = new URL(window.location.href);
  137.  
  138.     currentUrl.searchParams.set(`page`, page);
  139.     currentUrl.searchParams.set(`from`, (page - 1) * 50 + 1 == 0 ? 1 : (page - 1) * 50 + 1);
  140.     currentUrl.searchParams.set(`to`, (page) * 50);
  141.     window.location.href = currentUrl.href;
  142. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement