javascript - 在 HTML 表格中每页显示 N 行

标签 javascript jquery

你好,我有一个包含很多行的 html 表格,我使用 JavaScript 代码添加分页选项,工作正常但是当我加载文档时显示所有行,我只想显示 5、10 或其他但不是所有的行。这是我的 JavaScript 代码和可用的 Fiddle:

 $(document).ready(function () {
    getPagination('#Tabla');
});

function getPagination(table) {

  $('#maxRows').on('change', function() {
    $('.pagination').html(''); // reset pagination 
    var trnum = 0; // reset tr counter 
    var maxRows = parseInt($(this).val()); // get Max Rows from select option
    var totalRows = $(table + ' tbody tr').length; // numbers of rows 
    $(table + ' tr:gt(0)').each(function() { // each TR in  table and not the header
      trnum++; // Start Counter 
      if (trnum > maxRows) { // if tr number gt maxRows

        $(this).hide(); // fade it out 
      }
      if (trnum <= maxRows) {
        $(this).show();
      } // else fade in Important in case if it ..
    }); //  was fade out to fade it in 
    if (totalRows > maxRows) { // if tr total rows gt max rows option
      var pagenum = Math.ceil(totalRows / maxRows); // ceil total(rows/maxrows) to get ..  
      //    numbers of pages 
      for (var i = 1; i <= pagenum;) { // for each page append pagination li 
        $('.pagination').append('<li class"wp" data-page="' + i + '">\
                                      <span>' + i++ + '<span class="sr-only">(current)</span></span>\
                                    </li>').show();
      } // end for i 
    } // end if row count > max rows
    $('.pagination li:first-child').addClass('active'); // add active class to the first li 
    $('.pagination li').on('click', function() { // on click each page
      var pageNum = $(this).attr('data-page'); // get it's number
      var trIndex = 0; // reset tr counter
      $('.pagination li').removeClass('active'); // remove active class from all li 
      $(this).addClass('active'); // add active class to the clicked 
      $(table + ' tr:gt(0)').each(function() { // each tr in table not the header
        trIndex++; // tr index counter 
        // if tr index gt maxRows*pageNum or lt maxRows*pageNum-maxRows fade if out
        if (trIndex > (maxRows * pageNum) || trIndex <= ((maxRows * pageNum) - maxRows)) {
          $(this).hide();
        } else {
          $(this).show();
        } //else fade in 
      }); // end of for each tr in table
    }); // end of on click pagination list


    });
}

fiddle :

Working Code

最佳答案

我已经更改了您的代码,检查一下。创建分页的函数按原样工作。只是对 ni 代码做了一个小改动

 $(document).ready(function () {
        $('#maxRows').on('change', function() {
            getPagination('#Tabla',$(this).val());
        });
    getPagination('#Tabla',2); // the no of rows default you want to show
});

function getPagination(table,noRows) {

 $('.pagination').html(''); // reset pagination 
    var trnum = 0; // reset tr counter 
    var maxRows = noRows; // get Max Rows from select option
    var totalRows = $(table + ' tbody tr').length; // numbers of rows 
    $(table + ' tr:gt(0)').each(function() { // each TR in  table and not the header
      trnum++; // Start Counter 
      if (trnum > maxRows) { // if tr number gt maxRows

        $(this).hide(); // fade it out 
      }
      if (trnum <= maxRows) {
        $(this).show();
      } // else fade in Important in case if it ..
    }); //  was fade out to fade it in 
    if (totalRows > maxRows) { // if tr total rows gt max rows option
      var pagenum = Math.ceil(totalRows / maxRows); // ceil total(rows/maxrows) to get ..  
      //    numbers of pages 
      for (var i = 1; i <= pagenum;) { // for each page append pagination li 
        $('.pagination').append('<li class"wp" data-page="' + i + '">\
                                      <span>' + i++ + '<span class="sr-only">(current)</span></span>\
                                    </li>').show();
      } // end for i 
    } // end if row count > max rows
    $('.pagination li:first-child').addClass('active'); // add active class to the first li 
    $('.pagination li').on('click', function() { // on click each page
      var pageNum = $(this).attr('data-page'); // get it's number
      var trIndex = 0; // reset tr counter
      $('.pagination li').removeClass('active'); // remove active class from all li 
      $(this).addClass('active'); // add active class to the clicked 
      $(table + ' tr:gt(0)').each(function() { // each tr in table not the header
        trIndex++; // tr index counter 
        // if tr index gt maxRows*pageNum or lt maxRows*pageNum-maxRows fade if out
        if (trIndex > (maxRows * pageNum) || trIndex <= ((maxRows * pageNum) - maxRows)) {
          $(this).hide();
        } else {
          $(this).show();
        } //else fade in 
      }); // end of for each tr in table
    }); // end of on click pagination list
}

Update your Fiddle

关于javascript - 在 HTML 表格中每页显示 N 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39087619/

相关文章:

javascript - 如何使用 jQuery 来显示图像(如视频)

javascript - 如何在点击时动态更改 var 值?

javascript - 使用 Ionic 和 AngularJS 在 $scope 中更改变量值时, View 中的变量值未更新

javascript - 高效声明 JavaScript 变量

javascript - 阻止 Chrome 在我的网站上自动建议信用卡号码

javascript - 获取页面中的元素数

javascript - 如何使用 jQuery 提交表单?

javascript - 从 Reactjs 中的 onClick 事件渲染组件内的组件

jquery - 如何传递 jQuery 信用卡验证的表单值 pawel decowski

php - Jquery Php 发布值并根据结果获取结果