這應該工作。你可以試試嗎?
var countries = [];
var per_page = 10;
function generate_pagination(per_page) {
var pagination_links = countries.length/per_page;
var pagination_length = Math.ceil(pagination_links);
$('#pagination').html('');
for (var i = 1; i <= pagination_length; i++) {
$('#pagination').append('<a href="#" class="page">' + i + '</a>');
}
}
function display_countries(start, end) {
$('.new').html('');
for (var i = 0; i < countries.length; i++) {
if (i >= start && i < end) {
$('.new').append('<tr><td>' + (i + 1) + ": " + countries[i] + '</td><td>Action</td></tr>');
}
}
}
$(document).ready(function() {
$("#displayvalues").change(function() {
per_page = $(this).find("option:selected").val()
var end = 1 * per_page;
var start = 0;
display_countries(start, end);
generate_pagination(per_page);
})
$('body').on('click', '.page', function() {
var page_no = $(this).html();
var end = page_no * per_page;
var start = end - per_page;
display_countries(start, end);
return false;
})
$.ajax({
url: './countries.php',
type: 'GET',
data: { countries: true },
cache: false,
async: true,
dataType: 'json',
success: function (data) {
countries = data.countries;
generate_pagination(per_page);
display_countries(0, per_page);
}
});
});