HTML Table Pagination Filtering JavaScript

html table pagination filtering javascript
html table pagination filtering javascript

As websites and applications get more complex, displaying large amounts of data in a user-friendly way becomes a challenging task. One way to deal with this problem is by using HTML tables. However, tables with many rows and columns can be overwhelming to users and can slow down the performance of the web page. To solve this issue, developers can implement pagination and filtering functionality using JavaScript.

In this article, we’ll explore how to add pagination and filtering to an HTML table using JavaScript. We’ll also discuss some best practices for implementing this functionality to create a fast and responsive user experience.

What is HTML Table Pagination and Filtering?

Pagination is the practice of dividing data into smaller chunks, typically by splitting it into pages. When implementing pagination for an HTML table, we break the table into smaller tables, each containing a specific number of rows. By doing this, we can make the table more manageable for users and speed up the performance of the web page.

Filtering, on the other hand, is the practice of narrowing down the data displayed on the web page based on user-defined criteria. For example, if we have a table of customer data, we might allow users to filter the data based on location, age, or gender. This helps users find the data they need quickly and efficiently.

Implementing Pagination and Filtering with JavaScript

To implement pagination and filtering with JavaScript, we first need to understand the structure of an HTML table. A table consists of rows and columns, with each cell containing data. We can use JavaScript to access and manipulate the table’s elements to add pagination and filtering functionality.


To implement pagination, we need to split the table into smaller tables, each containing a specific number of rows. We can do this using JavaScript by creating a new table and copying the relevant rows from the original table.

First, we need to calculate the number of pages we’ll need based on the total number of rows and the desired number of rows per page. We can then create a new table for each page and copy the relevant rows from the original table to the new table. We can use event listeners to detect when the user clicks on the next or previous page buttons and update the table accordingly.

// Calculate the number of pages and rows per page
var rowsPerPage = 10;
var totalRows = document.getElementsByTagName('tr').length;
var totalPages = Math.ceil(totalRows / rowsPerPage);

// Create a new table for each page and copy the relevant rows
for (var i = 0; i < totalPages; i++) {
  var newTable = document.createElement('table');
  var startIndex = i * rowsPerPage;
  var endIndex = startIndex + rowsPerPage;
  var rowsToCopy ='tr'), startIndex, endIndex);

  rowsToCopy.forEach(function(row) {
    var newRow = document.createElement('tr');
    var cells = row.getElementsByTagName('td');, function(cell) {
      var newCell = document.createElement('td');
      newCell.innerHTML = cell.innerHTML;


To implement filtering, we need to allow users to select criteria that determine which rows should be displayed in the table. We can use JavaScript to filter the table’s rows based on the user’s selections.

First, we need to create an input field or dropdown menu that allows users to select the criteria to filter by. We can then add an event listener that listens for changes to the input field or dropdown menu and filters the table accordingly.

Here’s an example code for implementing filtering in an HTML table using JavaScript:

<!DOCTYPE html>
    <title>Filtering an HTML Table with JavaScript</title>
    <input type="text" id="filterInput" placeholder="Search...">
    <table id="myTable">
          <td>John Doe</td>
          <td>New York</td>
          <td>Jane Doe</td>
          <td>Los Angeles</td>
          <td>Bob Smith</td>
          <td>San Francisco</td>
          <td>Alice Johnson</td>
      // Get the input field and table
      var filterInput = document.getElementById('filterInput');
      var table = document.getElementById('myTable');
      var rows = table.getElementsByTagName('tr');

      // Add event listener to input field
      filterInput.addEventListener('keyup', function() {
        var filterValue = filterInput.value.toUpperCase();

        // Loop through all table rows
        for (var i = 0; i < rows.length; i++) {
          var cells = rows[i].getElementsByTagName('td');
          var showRow = false;

          // Loop through all table cells in a row
          for (var j = 0; j < cells.length; j++) {
            var cell = cells[j];
            if (cell.innerHTML.toUpperCase().indexOf(filterValue) > -1) {
              showRow = true;

          // Show or hide the row based on the filter value
          if (showRow) {
            rows[i].style.display = '';
          } else {
            rows[i].style.display = 'none';

In this example, we have an HTML table with four rows of data. We also have an input field that allows users to enter search criteria to filter the table.

When the user types in the input field, the JavaScript code loops through all the rows and cells in the table to find any cells that contain the filter value. If a cell contains the filter value, the corresponding row is shown; otherwise, the row is hidden using the style.display property.

This code will filter the table as the user types in the input field, allowing them to quickly find the data they need.

By umarbwn