【发布时间】:2020-09-27 16:03:35
【问题描述】:
如何在列表页面中添加一个选项,例如每页带有限制的项目(带有选项 10、25、50 的下拉菜单)。 它应该类似于引导数据表中的 show entries 选项。我正在使用 twbspagination.js 进行分页。这是示例链接js-tutorials,下面是源代码。
<body>
<div class="container" style="padding:10px 20px;">
<h2>Pagination Example Using jQuery</h2>
<table id="employee" class="table table-bordered table table-hover" cellspacing="0" width="100%">
<colgroup><col width="20%"><col width="35%"><col width="40%"></colgroup>
<thead>
<tr>
<th>Name</th>
<th >Salary</th>
<th>Age</th>
</tr>
</thead>
<tbody id="emp_body">
</tbody>
</table>
<div id="pager">
<ul id="pagination" class="pagination-sm"></ul>
</div>
</div>
</body>
<script type="text/javascript">
$(document).ready(function(){
var $pagination = $('#pagination'),
totalRecords = 0,
records = [],
displayRecords = [],
recPerPage = 10,
page = 1,
totalPages = 0;
$.ajax({
url: "https://www.js-tutorials.com/source_code/api_data/employee_all.php",
async: true,
dataType: 'json',
success: function (data) {
records = data;
console.log(records);
totalRecords = records.length;
totalPages = Math.ceil(totalRecords / recPerPage);
apply_pagination();
}
});
function generate_table() {
var tr;
$('#emp_body').html('');
for (var i = 0; i < displayRecords.length; i++) {
tr = $('<tr/>');
tr.append("<td>" + displayRecords[i].employee_name + "</td>");
tr.append("<td>" + displayRecords[i].employee_salary + "</td>");
tr.append("<td>" + displayRecords[i].employee_age + "</td>");
$('#emp_body').append(tr);
}
}
function apply_pagination() {
$pagination.twbsPagination({
totalPages: totalPages,
visiblePages: 6,
onPageClick: function (event, page) {
displayRecordsIndex = Math.max(page - 1, 0) * recPerPage;
endRec = (displayRecordsIndex) + recPerPage;
console.log(displayRecordsIndex + 'ssssssssss'+ endRec);
displayRecords = records.slice(displayRecordsIndex, endRec);
generate_table();
}
});
}
});
</script>
【问题讨论】:
标签: javascript html jquery ajax