How To Pagination In Javascript

In this blog post, we will learn how to create a simple pagination system using JavaScript. Pagination is an essential feature for any web application that deals with large datasets, as it allows users to navigate through the data easily and efficiently.

Prerequisites

To follow this tutorial, you should have basic knowledge of HTML, CSS, and JavaScript. No external libraries are required, and we will use vanilla JavaScript for this example.

Getting Started

First, let’s create a simple HTML table to display our data. For this tutorial, we will use an array of objects to represent our data. Add the following code to your HTML file:

<table id="data-table" class="table">
  <thead>
    <tr>
      <th>ID</th>
      <th>Name</th>
      <th>Email</th>
    </tr>
  </thead>
  <tbody></tbody>
</table>

Next, let’s create an array of objects to represent our sample data:

const data = [
  { id: 1, name: &#039;John Doe&#039;, email: &#039;[email protected]&#039; },
  { id: 2, name: &#039;Jane Doe&#039;, email: &#039;[email protected]&#039; },
  // ... more data
];

Creating the Pagination Function

Now that we have our data and table set up, we can create a function to handle the pagination logic. The function will have three parameters:

  • data: the data array to be paginated
  • pageSize: the number of items to display per page
  • currentPage: the current page number

Here is the code for our pagination function:

function paginate(data, pageSize, currentPage) {
  const startIndex = (currentPage - 1) * pageSize;
  const endIndex = startIndex + pageSize;
  return data.slice(startIndex, endIndex);
}

Rendering the Paginated Data

Now that we have our pagination function, we can use it to render the paginated data into our HTML table. Add the following code to your JavaScript file:

function renderTable(data, pageSize, currentPage) {
  const tableBody = document.getElementById(&#039;data-table&#039;).getElementsByTagName(&#039;tbody&#039;)[0];
  tableBody.innerHTML = &#039;&#039;;

  const paginatedData = paginate(data, pageSize, currentPage);
  
  paginatedData.forEach(item =&amp;gt; {
    const row = tableBody.insertRow();
    row.innerHTML = `
      &amp;lt;td&amp;gt;${item.id}&amp;lt;/td&amp;gt;
      &amp;lt;td&amp;gt;${item.name}&amp;lt;/td&amp;gt;
      &amp;lt;td&amp;gt;${item.email}&amp;lt;/td&amp;gt;
    `;
  });
}

// Example usage:
renderTable(data, 5, 1);

Creating the Pagination Controls

Finally, let’s create the pagination controls (previous, next, and page numbers) to allow users to navigate through the paginated data. Add the following HTML code below your table:

<nav>
  <ul id="pagination-controls" class="pagination"></ul>
</nav>

Next, add the following JavaScript code to create the pagination controls:

function createPaginationControls(data, pageSize, currentPage) {
  const totalPages = Math.ceil(data.length / pageSize);
  const paginationControls = document.getElementById(&#039;pagination-controls&#039;);

  paginationControls.innerHTML = &#039;&#039;;

  // Create previous button
  const previousButton = document.createElement(&#039;li&#039;);
  previousButton.className = &#039;page-item&#039; + (currentPage === 1 ? &#039; disabled&#039; : &#039;&#039;);
  previousButton.innerHTML = &#039;&amp;lt;a class=&quot;page-link&quot; href=&quot;#&quot;&amp;gt;Previous&amp;lt;/a&amp;gt;&#039;;
  previousButton.onclick = () =&amp;gt; {
    if (currentPage &amp;gt; 1) {
      updateTableAndControls(currentPage - 1);
    }
  };
  paginationControls.appendChild(previousButton);

  // Create page number buttons
  for (let i = 1; i &amp;lt;= totalPages; i++) {
    const pageButton = document.createElement(&#039;li&#039;);
    pageButton.className = &#039;page-item&#039; + (i === currentPage ? &#039; active&#039; : &#039;&#039;);
    pageButton.innerHTML = `&amp;lt;a class=&quot;page-link&quot; href=&quot;#&quot;&amp;gt;${i}&amp;lt;/a&amp;gt;`;
    pageButton.onclick = () =&amp;gt; updateTableAndControls(i);
    paginationControls.appendChild(pageButton);
  }

  // Create next button
  const nextButton = document.createElement(&#039;li&#039;);
  nextButton.className = &#039;page-item&#039; + (currentPage === totalPages ? &#039; disabled&#039; : &#039;&#039;);
  nextButton.innerHTML = &#039;&amp;lt;a class=&quot;page-link&quot; href=&quot;#&quot;&amp;gt;Next&amp;lt;/a&amp;gt;&#039;;
  nextButton.onclick = () =&amp;gt; {
    if (currentPage &amp;lt; totalPages) {
      updateTableAndControls(currentPage + 1);
    }
  };
  paginationControls.appendChild(nextButton);
}

function updateTableAndControls(newPage) {
  renderTable(data, 5, newPage);
  createPaginationControls(data, 5, newPage);
}

// Example usage:
createPaginationControls(data, 5, 1);

Conclusion

In this tutorial, we learned how to create a simple pagination system using JavaScript. You can now apply these techniques to your own web applications to provide a better user experience when dealing with large datasets. Happy coding!