Hello everyone,
Can anyone help me to add pagination to an existing html table using javascript with pervious and next button and all page number?
Hello everyone,
Can anyone help me to add pagination to an existing html table using javascript with pervious and next button and all page number?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Mar 30, 2023, 1:44 AM EDT
Check this example of how to add pagination to an existing HTML table using JavaScript.
Add a div for pagination to your code.
Script to initialize the pagination
const table = document.getElementById('table1'); const paginationContainer = document.getElementById('pagination'); const rowsPerPage = 10; // Change this to set the number of rows per page let displayPageNo = 1; const totalRows = table.tBodies[0].rows.length; const numPages = Math.ceil(totalRows / rowsPerPage); function paginate() { // Create pagination buttons const pageNumbers = document.createElement('ul'); pageNumbers.classList.add("pagination", "pagination-month", "justify-content-center"); const prevList = document.createElement('li'); prevList.classList.add("page-item"); prevList.id = "prevPage"; const prevAnchor = document.createElement('a'); prevAnchor.classList.add("page-link"); prevAnchor.href = "javascript::"; prevAnchor.innerText = "«"; prevAnchor.addEventListener('click', () => { displayPage(displayPageNo - 1); }); prevList.appendChild(prevAnchor); pageNumbers.appendChild(prevList); for (let i = 1; i { displayPage(i); }); const paragraph = document.createElement('p'); paragraph.classList.add("page-month"); paragraph.innerText = i; anchor.appendChild(paragraph); list.appendChild(anchor); pageNumbers.appendChild(list); } const nextList = document.createElement('li'); nextList.classList.add("page-item"); nextList.id = "nextPage"; const nextAnchor = document.createElement('a'); nextAnchor.classList.add("page-link"); nextAnchor.href = "javascript::"; nextAnchor.innerText = "»"; nextAnchor.addEventListener('click', () => { displayPage(displayPageNo + 1); }); nextList.appendChild(nextAnchor); pageNumbers.appendChild(nextList); paginationContainer.appendChild(pageNumbers); displayPage(1); } function displayPage(pageNum) { const start = (pageNum - 1) * rowsPerPage; const end = start + rowsPerPage; const rows = table.tBodies[0].rows; const listPrev = document.getElementById('prevPage'); const listNext = document.getElementById('nextPage'); displayPageNo = pageNum; if (pageNum == numPages) { listNext.classList.add("page-item", "disabled"); listPrev.classList.remove("disabled"); } else if (pageNum == 1) { listNext.classList.remove("disabled"); listPrev.classList.add("page-item", "disabled"); } else { listNext.classList.remove("disabled"); listPrev.classList.remove("disabled"); } // Hide all rows for (let i = 0; iRajkiran SwainRank #165 · 14.4K reputationPosted Mar 29, 2023, 4:21 PM EDT
Here is a basic example of how to add pagination to an existing HTML table using JavaScript:
const table = document.querySelector('#my-table'); const tableRows = table.querySelectorAll('tr'); const pageSizeSelect = document.querySelector('#page-size-select'); const paginationContainer = document.querySelector('#pagination-container'); let currentPage = 1; let pageSize = Number(pageSizeSelect.value); function updateTable() { const startIndex = (currentPage - 1) * pageSize; const endIndex = startIndex + pageSize; tableRows.forEach((row, index) => { if (index > 0) { // skip header row row.style.display = (index > startIndex && index { event.preventDefault(); currentPage = i; updateTable(); }); paginationContainer.appendChild(link); } } pageSizeSelect.addEventListener('change', event => { pageSize = Number(event.target.value); currentPage = 1; updateTable(); }); updateTable();Vishal YelveRank #178 · 13.6K reputationPosted Mar 29, 2023, 9:37 AM EDT
Hi Deepak,
Do refer below links
https://getbutterfly.com/client-side-html-pagination-with-javascript/
https://stackoverflow.com/questions/19605078/how-to-use-pagination-on-html-tables
Raj BhattRank #513 · 4.8K reputationPosted Mar 30, 2023, 2:04 AM EDT
Hi ,
You can Try Below Example