I have a datatable which I should change something on it, for example I want to change the status of a content, but this content is in 3rd page on the table. When I change it, datatable refreshes itself to the 1st page. What I'm trying to do is to keep the selected page number and call it back after refresh. Is that possible?
btw, I'm using datatables 1.9.4
EDIT: SOLUTION
What I've done is simply keeping the page number in every action that I make in datatable and sending it to the Controller and then using it via TempData. If anyone needs a hand about the solution, just make me know, I can explain more detailed.
I save the datatable state in the local storage to avoid passing the page number all over my app. This is how I do it:
$('#offersTable').dataTable({
"bStateSave": true,
"fnStateSave": function (oSettings, oData) {
localStorage.setItem('offersDataTables', JSON.stringify(oData));
},
"fnStateLoad": function (oSettings) {
return JSON.parse(localStorage.getItem('offersDataTables'));
}
});
This is very useful when you go to another page and you want to go back (using the back button) to the last selected page.
See also the documentation: https://datatables.net/blog/2012-01-16
In DataTables 1.10 you have an ability to stay on the same page after re-drawing if you pass false
as a first parameter to the draw()
function.
table.row(index).data(data).draw(false)
Here is how to do it in DataTable v1.10.7:
DataTable Storing the state in a cookie for you.
$(document).ready(function() {
$('#example').dataTable( {
stateSave: true
} );
} );
If you are using server side datatable, then you can use ajax.reload()
function to reload the datatable.
Use like
var dt = $("#table").DataTable();
dt.ajax.reload(null, false); // false if you don't want to refresh paging else true.
See https://datatables.net/reference/api/ajax.reload() link.
If you are update any data using ajax on page number 3 and you don't want that after refresh my data in data table it will go to first page again so here is the solution that will refresh data of table but not redirect you to first page. it will be reflect changes on same page.
This below will not redirect to first page after refresh/reload data table using ajax
$('#YOUR_TABLE_ID').DataTable().ajax.reload(null, false); //without refresh table<<page will not change>>
This below will redirect to first page after refresh/reload data table using ajax
$('#YOUR_TABLE_ID').DataTable().ajax.reload(); //with refresh table <<page will change>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With