我有一个脚本分页表中的页面,工作正常。我还有一个下拉列表,可以选择要在表格中显示的记录数。虽然这有效,但如果我选择说20条记录,则分页不会更新以反映更改。
当表格运行时,要显示的默认记录为8,即9页。但如果我在下拉列表中选择20,它仍会显示9,如果我转到第9页,它显然是空的。我也想知道是否可以在表格之前和之后出现脚本。对此的共鸣是,如果用户达到说100条记录的底部,那么我想要这个功能,所以他们不必一直回到顶部。
我很感激任何帮助,因为我的javascript不是100%。非常感谢
UPDATE:从我收集的内容中,我必须重新渲染repaginate()函数。保持它的位置,但移动在repaginate()函数内呈现实际页面的位。试过各种选择,但没有工作。我知道如何做到这一点,因为我真的在努力。感谢
php html
<?php
echo "<table class='paginated' cellspacing='0' cellpadding='0'>";
echo "<div class='pager'></div>";
echo "<thead><tr><th style='width: 5%;'>#</th><th>Service</th><th>Activity</th><th>Dept</th><th>Company</th><th>Address</th><th>User</th><th>Item</th><th>Date</th><th style='width: 19%;'>Action</th></tr></thead>";
while($row = mysqli_fetch_array($result)){
$id = $row['id'];
$service = $row['service'];
$activity = $row['activity'];
$dept = $row['department'];
$company = $row['company'];
$address = $row['address'];
$user = $row['user'];
$box = $row['item'];
$date = $row['date'];
$date = date('d-m-Y h:i:s', strtotime($date));
$edit = "<button type='button' class='btn btn-primary edit'>Edit</button>";
$action = "<button type='button' class='btn btn-success action'>Action</button>";
$delete = "<button type='button' class='btn btn-danger delete'>Delete</button>";
// each looped row
echo "<tr><td>".$id."</td><td>".$service."</td><td>".$activity."</td><td>".$dept."</td><td>".$company."</td><td>".$address."</td><td>".$user."</td><td>".$box."</td><td>".$date."</td><td>".$edit. ' ' .$action. ' ' .$delete."</td></tr>";
} // End while loop
echo "</table><br /><br /></div>";
?>
JS
$('table.paginated').each(function() {
var currentPage = 0;
var numPerPage = 8;
var $table = $(this);
$table.bind('repaginate', function() {
$table.find('tbody tr').hide().slice(currentPage * numPerPage, (currentPage + 1) * numPerPage).show();
});
$table.trigger('repaginate');
var numRows = $table.find('tbody tr').length;
var numPages = Math.ceil(numRows / numPerPage);
var $pager = $('<div class="pager"></div>');
for (var page = 0; page < numPages; page++) {
$('<span class="page-number"></span>').text(page + 1).bind('click', {
newPage: page
}, function(event) {
currentPage = event.data['newPage'];
$table.trigger('repaginate');
$(this).addClass('active').siblings().removeClass('active');
}).appendTo($pager).addClass('clickable');
}
$pager.insertBefore($table).find('span.page-number:first').addClass('active');
var $numberPicker = $('<div class="numberPicker"></div>');
var dropdown = $('<select class="options"></select>');
$([5, 10, 20]).each(function() {
var $num = this;
$('<option></option>').text(this).attr('value', this).appendTo(dropdown);
});
dropdown.bind('change', function() {
numPerPage = this.value;
$table.trigger('repaginate');
}).insertBefore($table);
});