duanou9758 2017-11-08 09:23
浏览 90

使用下一个和上一个按钮分页,并且仅使用jQuery在HTML表格中加载显示5页

I got a no. of pages but I want to restrict it by showing 5 pages on load with next and previous buttons which shows pages number.

My code is:

  <script>
    $(document).ready(function() {
      var totalRows = $('#MyTable').find('tbody tr:has(td)').length;
      var recordPerPage = 10;
      var totalPages = Math.ceil(totalRows / recordPerPage);
      var $pages = $('<div id="pages"></div>');
      var $previous = $('<span class="previous"><<</spnan>');
      var $next = $('<span class="next">>></spnan>');
      for (i = 0; i < totalPages; i++) {
         $('<span>' + (i + 1) + '</span>').appendTo($pages);     
      }
      $pages.appendTo('#MyTable');

      $('#MyTable').find('tbody tr:has(td)').hide();
      var tr = $('#MyTable tbody tr:has(td)');
      for (var i = 0; i <= recordPerPage-1; i++) {
          $(tr[i]).show();
      }
      $('span').click(function(event) {
        $('#MyTable').find('tbody tr:has(td)').hide();
        var nBegin = ($(this).text() - 1) * recordPerPage;
        var nEnd = $(this).text() * recordPerPage - 1;
        for (var i = nBegin; i <= nEnd; i++)
        {
          $(tr[i]).show();
        }
      });
    });
    </script>

I've made my pagination like: "first previous 1 2 3 4 5 next last"

I hope you guys understand?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dqvtm82066 2017-11-08 10:00
    关注

    Hi, You can create simple pagination using the TWBS pagination JS library. here is a quick example of pagination on codepen it might help you.

    example code on codepen
    

    https://codepen.io/SitePoint/pen/WprNwa/

    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)