I have a pagination script that is working fine under normal events. ie with no ajax calls etc. What I am doing is performing a search and when the results are displayed, the pagination is not getting updated. What I need to happen is when the search is finished and the results displaye, is to update the numer of pages to reflect the results of the search. For example if there is 1 result only show 1 page. if there are 10 results then update the number of paes based on the results.
I would be grateful if someone could help with this. Many thanks
js code
$(function() {
$('table.paginated').each(function() {
var currentPage = 0;
var numPerPage = 8;
var $table = $(this);
var numRows = $table.find('tbody tr').length;
var numPages = Math.ceil(numRows / numPerPage);
var $pager1 = $('<div class="pager"></div>');
var $pager2 = "";
var $numberPicker = $('<div class="numberPicker"></div>');
var dropdown = $('<select class="options"></select>');
var dropdown2 = "";
$table.bind('repaginate', function() {
$table.find('tbody tr').hide().slice(currentPage * numPerPage, (currentPage + 1) * numPerPage).show();
});
$table.trigger('repaginate');
function rePage(numPages) {
$('.page-number').remove();
for (var page = 0; page < numPages; page++) {
$('<span class="page-number"></span>').text(page + 1).bind('click', {
newPage: page
}, function(event) {
var selected = $(this).index();
var selected2 = ($(this).index()) + numPages;
currentPage = event.data['newPage'];
$table.trigger('repaginate');
$('span.page-number').removeClass('active');
$("span.page-number").each(function(index) {
if (index === selected) {
$(this).addClass('active');
$("span.page-number:eq(" + selected2 + ")").addClass('active')
}
});
}).appendTo($pager1).addClass('clickable');
}
$pager2 = $pager1.clone(true);
$pager2.insertAfter($table);
}
rePage(numPages);
$pager1.insertBefore($table);
$pager2.insertAfter($table);
$('span.page-number:first-child').addClass('active');
$([5, numPerPage, 10, 20]).each(function() {
var $num = this;
$('<option></option>').text(this).attr('value', this).appendTo(dropdown);
});
dropdown.bind('change', function() {
var oldNumPerPage = numPerPage;
numPerPage = this.value;
numPages = Math.ceil(numRows / numPerPage);
$table.trigger('repaginate');
rePage(numPages);
currentPage = Math.ceil((currentPage * oldNumPerPage) / numPerPage);
$("span.page-number:eq(" + currentPage + ")").trigger('click');
$('.options').val(numPerPage);
$('.options2').val(numPerPage);
}).insertBefore($pager2);
dropdown2 = dropdown.clone(true);
dropdown2.addClass('options2');
dropdown2.insertAfter($pager1);
$('.options').val(numPerPage);
$('.options2').val(numPerPage);
});
});
button search js
$(function() {
$('#btn-search').on('click', function(e) {
e.preventDefault();
var search = $("#search").val();
$.ajax({
url: "tabletestajax.php",
method: 'POST',
data: {
search1: search
},
success: function(data) {
$("#search").val('');
$(".paginated tbody").empty();
var result = $.trim(data);
if (result === "enter a search term") {
$("input[name='search']").val(result).css({
'color': 'red'
});
return false;
} else if (result === "No Results Found") {
$(".paginated tbody").html('<div>' + result + '</div>');
return false;
} else {
$(".paginated tbody").html(result);
}
}
});
});
});