Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
164
rated 0 times [  168] [ 4]  / answers: 1 / hits: 20907  / 11 Years ago, wed, september 4, 2013, 12:00:00

I have a JQuery datatable that loads data via an ajax request. There is a form by the table that lets the user filter the results on the server. When the user changes the filter form, I call fnReloadAjax() with the new filter data, and the table redraws with the new, filtered results.



The problem I have is that the pagination sticks, even if the table no longer has enough items to reach the current page. So if the table originally had 20 items, and the user was on page 2 (10 items per page), and they change the filter so only 5 items are returned, the table displays no items and shows this at the bottom:



Showing 11 to 5 of 5 entries


It's still on page 2 even though there is only enough data for one page.



I have found numerous posts about trying to preserve the current page/row, but none showing a simple way to reset pagination to the first page. What is the simplest way to do this?



Here's a simplified version of my code for clarity:



$(#mytable).dataTable({
bStateSave: false,
fnServerData: function (sSource, aoData, fnCallback) {
return $.ajax({
type: POST,
url: url,
data: {filter: filterValue}
});
}
});

$(#myForm).submit(function(e) {
table.fnReloadAjax();
return false;
});

More From » jquery

 Answers
15

You could explicitly jump to the first page after reloading, see http://datatables.net/api#fnPageChange



$(#myForm).submit(function(e) {
table.fnPageChange(0);
table.fnReloadAjax();
return false;
});

[#75915] Tuesday, September 3, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kennedim

Total Points: 699
Total Questions: 85
Total Answers: 105

Location: Saint Helena
Member since Mon, Jan 16, 2023
1 Year ago
;