Advertisement
Grossos

pagination-need

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