0

I have implemented a pagination feature on a table in my HTML page. Each page should display three rows, and I have a total of eight rows in my table. Additionally, I have a button that removes rows with the class "remove_tr". When this button is clicked, I want the corresponding rows to be removed and the pagination to be updated accordingly.

The issue I'm facing is that when I remove the rows with the "remove_tr" class, only the rows on the first page are affected. The pagination links and displayed rows are not updated correctly, and I end up with two pages instead of three. I need assistance in modifying the existing code to ensure that the removal of rows updates the pagination correctly, so that each page consistently shows three rows.

$(document).ready(function() {
  add_pagination();
});

function add_pagination() {
  var rowsPerPage = 3;
  var $table = $('#data-table');
  var $tbody = $table.find('tbody');
  var $rows = $tbody.find('tr');
  var totalRows = $rows.length;
  var totalPages = Math.ceil(totalRows / rowsPerPage);
  var $pagination = $('.pagination');

  var $pagination = $('<div class="pagination"></div>'); // إنشاء عنصر الترقيم
  $table.after($pagination);

  for (var i = 1; i <= totalPages; i++) {
    var $link = $('<a href="#"></a>').text(i);
    $link.attr('data-page', i);
    $pagination.append($link);
  }

  $rows.hide();
  $rows.slice(0, rowsPerPage).show();
  $pagination.find('a:first').addClass('active');

  $pagination.on('click', 'a', function(e) {
    e.preventDefault();
    var page = $(this).attr('data-page');
    var startIndex = (page - 1) * rowsPerPage;
    var endIndex = startIndex + rowsPerPage;

    $rows.hide();
    $rows.slice(startIndex, endIndex).show();

    $pagination.find('a').removeClass('active');
    $(this).addClass('active');
  });

}


$(".remove7").on("click", function() {
  $(".remove_tr").remove();
})
.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.pagination a {
  margin: 0 5px;
  padding: 5px 10px;
  background-color: #f2f2f2;
  color: #333;
  text-decoration: none;
  border-radius: 3px;
}

.pagination a.active {
  background-color: #4CAF50;
  color: white;
}
<button class="remove7">remove</button>
<table id="data-table">
  <thead>
    <tr id="table-header">
      <th data-column="ALARM_DESC" data-order="asc">
        ALARM_DESC
        <div class="resize-handle"></div>
      </th>
      <th data-column="ALARM_NAME" data-order="asc">
        ALARM_NAME
        <div class="resize-handle"></div>
      </th>
      <th data-column="APPLICAITON" data-order="asc">
        APPLICAITON
        <div class="resize-handle"></div>
      </th>
      <th data-column="ALARM_PATH" data-order="asc">
        ALARM_PATH
        <div class="resize-handle"></div>
      </th>
    </tr>
  </thead>
  <tbody id="table-body">
    <tr class="high-priority remove_tr">
      <td class="ALARM_DESC">first desc</td>
      <td class="ALARM_NAME">first name</td>
      <td class="APPLICAITON">first app</td>
      <td class="ALARM_PATH">first path</td>

    </tr>
    <tr class="high-priority remove_tr">
      <td class="ALARM_DESC">Second desc</td>
      <td class="ALARM_NAME">Second name</td>
      <td class="APPLICAITON">second app</td>
      <td class="ALARM_PATH">second path</td>

    </tr>

    <tr class="high-priority ">
      <td class="ALARM_DESC">third desc</td>
      <td class="ALARM_NAME">third name</td>
      <td class="APPLICAITON">third app</td>
      <td class="ALARM_PATH">third path</td>

    </tr>
    <tr class="high-priority">
      <td class="ALARM_DESC">four desc</td>
      <td class="ALARM_NAME">four name</td>
      <td class="APPLICAITON">four app</td>
      <td class="ALARM_PATH">four path</td>

    </tr>
    <tr class="high-priority">
      <td class="ALARM_DESC">fifth desc</td>
      <td class="ALARM_NAME">fifth name</td>
      <td class="APPLICAITON">fifth app</td>
      <td class="ALARM_PATH">fifth path</td>

    </tr>
    <tr class="high-priority">
      <td class="ALARM_DESC">six desc</td>
      <td class="ALARM_NAME">six name</td>
      <td class="APPLICAITON">six app</td>
      <td class="ALARM_PATH">six path</td>

    </tr>
  </tbody>
</table>

I have already implemented basic pagination functionality using JavaScript/jQuery, but I'm struggling to integrate the dynamic row removal with the pagination logic. I've included the relevant HTML and JavaScript code in my initial question.

Any help or suggestions on how to address this issue would be greatly appreciated. Thank you!

What I Tried: I attempted to modify the existing code by adding a click event handler to the "remove7" button. Within the event handler, I iterated through the visible rows and removed any rows that had the "remove_tr" class. I also calculated the number of rows removed on the first page.

What I Expected: Upon clicking the "remove7" button, I expected the rows with the "remove_tr" class to be removed from the first page only. The pagination should then update accordingly, displaying three rows per page and adjusting the pagination links.

What Actually Resulted: After executing the code, only the rows on the first page were correctly removed. However, the pagination links and displayed rows did not update as expected. The pagination still showed two pages instead of three, and the displayed rows were not consistent with the desired pagination logic.

2 Answers 2

0

Refresh the pagination after each update (I addded the class pagination to identify it:

$(".pagination").remove(); 
add_pagination();

You also had no rows with class remove_tr in the second page (I assigned remove_tr to some rows in the second page too and highlighted them).

Here's a demo:

$(document).ready(function() {
  add_pagination();
});

function add_pagination() {
  var rowsPerPage = 3;
  var $table = $('#data-table');
  var $tbody = $table.find('tbody');
  var $rows = $tbody.find('tr');
  var totalRows = $rows.length;
  var totalPages = Math.ceil(totalRows / rowsPerPage);
  var $pagination = $('.pagination');

  var $pagination = $('<div class="pagination"></div>'); // إنشاء عنصر الترقيم
  $table.after($pagination);

  for (var i = 1; i <= totalPages; i++) {
    var $link = $('<a href="#"></a>').text(i);
    $link.attr('data-page', i);
    $link.attr('class', 'pagination');
    $pagination.append($link);
  }

  $rows.hide();
  $rows.slice(0, rowsPerPage).show();
  $pagination.find('a:first').addClass('active');

  $pagination.on('click', 'a', function(e) {
    e.preventDefault();
    var page = $(this).attr('data-page');
    var startIndex = (page - 1) * rowsPerPage;
    var endIndex = startIndex + rowsPerPage;

    $rows.hide();
    $rows.slice(startIndex, endIndex).show();

    $pagination.find('a').removeClass('active');
    $(this).addClass('active');
  });

}


$(".remove7").on("click", function() {
  $(".remove_tr").remove();
  $(".pagination").remove();
  add_pagination();
})
.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.pagination a {
  margin: 0 5px;
  padding: 5px 10px;
  background-color: #f2f2f2;
  color: #333;
  text-decoration: none;
  border-radius: 3px;
}

.pagination a.active {
  background-color: #4CAF50;
  color: white;
}

.remove_tr {
  background-color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<button class="remove7">remove</button>
<table id="data-table">
  <thead>
    <tr id="table-header">
      <th data-column="ALARM_DESC" data-order="asc">
        ALARM_DESC
        <div class="resize-handle"></div>
      </th>
      <th data-column="ALARM_NAME" data-order="asc">
        ALARM_NAME
        <div class="resize-handle"></div>
      </th>
      <th data-column="APPLICAITON" data-order="asc">
        APPLICAITON
        <div class="resize-handle"></div>
      </th>
      <th data-column="ALARM_PATH" data-order="asc">
        ALARM_PATH
        <div class="resize-handle"></div>
      </th>
    </tr>
  </thead>
  <tbody id="table-body">
    <tr class="high-priority remove_tr">
      <td class="ALARM_DESC">first desc</td>
      <td class="ALARM_NAME">first name</td>
      <td class="APPLICAITON">first app</td>
      <td class="ALARM_PATH">first path</td>

    </tr>
    <tr class="high-priority remove_tr">
      <td class="ALARM_DESC">Second desc</td>
      <td class="ALARM_NAME">Second name</td>
      <td class="APPLICAITON">second app</td>
      <td class="ALARM_PATH">second path</td>

    </tr>

    <tr class="high-priority ">
      <td class="ALARM_DESC">third desc</td>
      <td class="ALARM_NAME">third name</td>
      <td class="APPLICAITON">third app</td>
      <td class="ALARM_PATH">third path</td>

    </tr>
    <tr class="high-priority">
      <td class="ALARM_DESC">four desc</td>
      <td class="ALARM_NAME">four name</td>
      <td class="APPLICAITON">four app</td>
      <td class="ALARM_PATH">four path</td>

    </tr>
    <tr class="high-priority">
      <td class="ALARM_DESC">fifth desc</td>
      <td class="ALARM_NAME">fifth name</td>
      <td class="APPLICAITON">fifth app</td>
      <td class="ALARM_PATH">fifth path</td>

    </tr>
    <tr class="high-priority remove_tr">
      <td class="ALARM_DESC">six desc</td>
      <td class="ALARM_NAME">six name</td>
      <td class="APPLICAITON">six app</td>
      <td class="ALARM_PATH">six path</td>

    </tr>
  </tbody>
</table>

0

<!DOCTYPE html>
<html>
<head>
  <title>Pagination with Dynamic Row Removal</title>
  <style>
    .pagination {
      display: flex;
      justify-content: center;
      margin-top: 20px;
    }

    .pagination a {
      margin: 0 5px;
      padding: 5px 10px;
      background-color: #f2f2f2;
      color: #333;
      text-decoration: none;
      border-radius: 3px;
    }

    .pagination a.active {
      background-color: #4caf50;
      color: white;
    }

    table {
      width: 100%;
      border-collapse: collapse;
    }

    table, th, td {
      border: 1px solid black;
    }

    th, td {
      padding: 8px;
      text-align: left;
    }

    .pagination button {
      margin: 0 5px;
      padding: 5px 10px;
    }
  </style>
</head>

<body>
  <table id="myTable">
    <thead>
      <tr>
        <th>ALARM_DESC</th>
        <th>ALARM_NAME</th>
        <th>APPLICATION</th>
        <th>ALARM_PATH</th>
        <th>Action</th>
      </tr>
    </thead>
    <tbody>
      <tr class="high-priority remove_tr">
        <td class="ALARM_DESC">first desc</td>
        <td class="ALARM_NAME">first name</td>
        <td class="APPLICATION">first app</td>
        <td class="ALARM_PATH">first path</td>
        <td><button class="remove7">Remove</button></td>
      </tr>
      <tr class="high-priority remove_tr">
        <td class="ALARM_DESC">Second desc</td>
        <td class="ALARM_NAME">Second name</td>
        <td class="APPLICATION">second app</td>
        <td class="ALARM_PATH">second path</td>
        <td><button class="remove7">Remove</button></td>
      </tr>
      <tr class="high-priority remove_tr">
        <td class="ALARM_DESC">third desc</td>
        <td class="ALARM_NAME">third name</td>
        <td class="APPLICATION">third app</td>
        <td class="ALARM_PATH">third path</td>
        <td><button class="remove7">Remove</button></td>
      </tr>
      <tr class="high-priority remove_tr">
        <td class="ALARM_DESC">four desc</td>
        <td class="ALARM_NAME">four name</td>
        <td class="APPLICATION">four app</td>
        <td class="ALARM_PATH">four path</td>
        <td><button class="remove7">Remove</button></td>
      </tr>
      <tr class="high-priority remove_tr">
        <td class="ALARM_DESC">fifth desc</td>
        <td class="ALARM_NAME">fifth name</td>
        <td class="APPLICATION">fifth app</td>
        <td class="ALARM_PATH">fifth path</td>
        <td><button class="remove7">Remove</button></td>
      </tr>
      <tr class="high-priority remove_tr">
        <td class="ALARM_DESC">six desc</td>
        <td class="ALARM_NAME">six name</td>
        <td class="APPLICATION">six app</td>
        <td class="ALARM_PATH">six path</td>
        <td><button class="remove7">Remove</button></td>
      </tr>
    </tbody>
  </table>
  <div class="pagination"></div>
  
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script>
    $(document).ready(function () {
      var rowsPerPage = 3;
      var $table = $('#myTable');
      var $pagination = $('.pagination');
      var currentPage = 1;

      function generatePagination() {
        $pagination.empty();
        var numRows = $table.find('tbody tr').length;
        var numPages = Math.ceil(numRows / rowsPerPage);

        for (var i = 1; i <= numPages; i++) {
          $pagination.append('<button class="page" data-page="' + i + '">' + i + '</button>');
        }
      }

      function showPage(page) {
        currentPage = page;
        $table.find('tbody tr').hide();
        $table.find('tbody tr').slice((currentPage - 1) * rowsPerPage, currentPage * rowsPerPage).show();
      }

      function updateTable() {
        generatePagination();
        showPage(currentPage);
      }

      $pagination.on('click', '.page', function () {
        var page = $(this).data('page');
        showPage(page);
      });

      $table.on('click', '.remove7', function () {
        $(this).closest('tr').remove();
        updateTable();
      });

      updateTable();
    });
  </script>
</body>
</html>

This code sets up a table with pagination and dynamic row removal. The table displays three rows per page. Pagination buttons allow the user to navigate between pages. When a row's "Remove" button is clicked, the row is removed from the table, and the pagination is updated accordingly.

updateTable() is called to set up the initial display of the table and pagination.

Not the answer you're looking for? Browse other questions tagged or ask your own question.