javascript - Jquery分页记录

标签 javascript jquery html

想问一下如何根据表中的记录显示正确的条目行(黄色背景部分)?例如,如果我选择每页“显示 5 行”,则结果将是“显示 1-5 行,共 {total 条目}”,当我点击第二页时,结果将是“显示 6-10 行,共 {总条目}”。希望有人可以教教我。

	$(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
	  }).trigger('change');

	  // end of on select change 
	  // END OF PAGINATION 
	}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<select class="form-control" name="state" id="maxRows">
	<option value="1" selected>Show 1 Rows</option>
	<option value="3">Show 3 Rows</option>
	<option value="5">Show 5 Rows</option>
	<option value="5000">Show ALL Rows</option>
</select>

<table id="Tabla">
	<thead>
		<tr>
			<th>Name</th>
			<th>Remark</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<th>Ali</th>
			<th>Test1</th>
		</tr>
		<tr>
			<th>Abu</th>
			<th>Test2</th>
		</tr>
		<tr>
			<th>Ali</th>
			<th>Test1</th>
		</tr>
		<tr>
			<th>Abu</th>
			<th>Test2</th>
		</tr>
		<tr>
			<th>Ali</th>
			<th>Test1</th>
		</tr>
		<tr>
			<th>Abu</th>
			<th>Test2</th>
		</tr>
		
		<tr>
			<th>Ali</th>
			<th>Test1</th>
		</tr>
		<tr>
			<th>Abu</th>
			<th>Test2</th>
		</tr>
		<tr>
			<th>Ali</th>
			<th>Test1</th>
		</tr>
		<tr>
			<th>Abu</th>
			<th>Test2</th>
		</tr>
		<tr>
			<th>Ali</th>
			<th>Test1</th>
		</tr>
		<tr>
			<th>Abu</th>
			<th>Test2</th>
		</tr>
		<tr>
			<th>Ali</th>
			<th>Test1</th>
		</tr>
		<tr>
			<th>Abu</th>
			<th>Test2</th>
		</tr>
		<tr>
			<th>Ali</th>
			<th>Test1</th>
		</tr>
		<tr>
			<th>Abu</th>
			<th>Test2</th>
		</tr>
		<tr>
			<th>Ali</th>
			<th>Test1</th>
		</tr>
		<tr>
			<th>Abu</th>
			<th>Test2</th>
		</tr>
		<tr>
			<th>Ali</th>
			<th>Test1</th>
		</tr>
		<tr>
			<th>Abu</th>
			<th>Test2</th>
		</tr>
		<tr>
			<th>Ali</th>
			<th>Test1</th>
		</tr>
		<tr>
			<th>Abu</th>
			<th>Test2</th>
		</tr>
		<tr>
			<th>Ali</th>
			<th>Test1</th>
		</tr>
		<tr>
			<th>Abu</th>
			<th>Test2</th>
		</tr>
		<tr>
			<th>Ali</th>
			<th>Test1</th>
		</tr>
	</tbody>
</table>

<div style="float:left">
	<strong>Showing <i style="background-color:yellow">11 to 17 </i>out of <?=$count2?> Entries </strong> 
</div>

<div>
	<nav>
	  <ul class="pagination" style="cursor:pointer"></ul>
	</nav>
</div>

最佳答案

检查一下......刚刚使用了一些数学知识,即操作保存页面总行数的total_rows变量

查看下面更新后的代码:

var initial = 0;
$(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 
    //console.log("maxRows---", maxRows, totalRows);
    $('#pagin').html(initial + " - " + maxRows);
    $('#totalData').html(totalRows);

    $(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 total_rows = maxRows * pageNum; //get total no. of rows WRT page
      $('#pagin').html(total_rows - maxRows + " - " + total_rows);
      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
  }).trigger('change');

  // end of on select change 
  // END OF PAGINATION 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<select class="form-control" name="state" id="maxRows">
  <option value="1" selected>Show 1 Rows</option>
  <option value="3">Show 3 Rows</option>
  <option value="5">Show 5 Rows</option>
  <option value="5000">Show ALL Rows</option>
</select>

<table id="Tabla">
  <thead>
    <tr>
      <th>Name</th>
      <th>Remark</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>Ali</th>
      <th>Test1</th>
    </tr>
    <tr>
      <th>Abu</th>
      <th>Test2</th>
    </tr>
    <tr>
      <th>Ali</th>
      <th>Test1</th>
    </tr>
    <tr>
      <th>Abu</th>
      <th>Test2</th>
    </tr>
    <tr>
      <th>Ali</th>
      <th>Test1</th>
    </tr>
    <tr>
      <th>Abu</th>
      <th>Test2</th>
    </tr>

    <tr>
      <th>Ali</th>
      <th>Test1</th>
    </tr>
    <tr>
      <th>Abu</th>
      <th>Test2</th>
    </tr>
    <tr>
      <th>Ali</th>
      <th>Test1</th>
    </tr>
    <tr>
      <th>Abu</th>
      <th>Test2</th>
    </tr>
    <tr>
      <th>Ali</th>
      <th>Test1</th>
    </tr>
    <tr>
      <th>Abu</th>
      <th>Test2</th>
    </tr>
    <tr>
      <th>Ali</th>
      <th>Test1</th>
    </tr>
    <tr>
      <th>Abu</th>
      <th>Test2</th>
    </tr>
    <tr>
      <th>Ali</th>
      <th>Test1</th>
    </tr>
    <tr>
      <th>Abu</th>
      <th>Test2</th>
    </tr>
    <tr>
      <th>Ali</th>
      <th>Test1</th>
    </tr>
    <tr>
      <th>Abu</th>
      <th>Test2</th>
    </tr>
    <tr>
      <th>Ali</th>
      <th>Test1</th>
    </tr>
    <tr>
      <th>Abu</th>
      <th>Test2</th>
    </tr>
    <tr>
      <th>Ali</th>
      <th>Test1</th>
    </tr>
    <tr>
      <th>Abu</th>
      <th>Test2</th>
    </tr>
    <tr>
      <th>Ali</th>
      <th>Test1</th>
    </tr>
    <tr>
      <th>Abu</th>
      <th>Test2</th>
    </tr>
    <tr>
      <th>Ali</th>
      <th>Test1</th>
    </tr>
  </tbody>
</table>

<div style="float:left">
  <strong>Showing <i id="pagin" style="background-color:yellow"></i> out of <span id="totalData"></span> Entries </strong>
</div>

<div>
  <nav>
    <ul class="pagination" style="cursor:pointer"></ul>
  </nav>
</div>

关于javascript - Jquery分页记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54414304/

相关文章:

javascript - 从 AsyncStorage 检索 JWT 以使用 ApolloClient 创建 httpLink 中间件时出现问题

javascript - 如何根据开始和滚动显示和隐藏菜单

javascript - 重新启用禁用菜单 "save video as"chrome ext 或 chrome 本身 "Save Video As"

javascript - 错误的赋值、逻辑运算符

javascript - 从不同的 .js 文件中访问 1 个 .js 源文件 (jqTree) 中的变量,以覆盖方法

javascript - 更改 jquery 模式确认对话框的大小

html - 我可以禁用 HTML 中的地址链接吗?

html - 如何设置圆形图像?

javascript - JQUERY AJAX 加载文本框焦点

jquery - 如何使用<spring :message tag in javascript/jQuery/jsp?