Datatables - Keeping selected page number after ca

2019-03-14 07:43发布

问题:

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.

回答1:

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



回答2:

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)


回答3:

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.



回答4:

fnStandingRedraw is the one you want. http://www.craiglotter.co.za/2012/05/28/how-to-refresh-a-datatable-without-losing-your-current-page-or-ordering/



回答5:

Redraw the table maintaining current paging position:

var table = $('#example').DataTable();
table.draw( false );