Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datatables - Keeping selected page number after callback

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.

like image 300
Burak Karakuş Avatar asked Aug 06 '14 10:08

Burak Karakuş


5 Answers

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

like image 105
Francisco Goldenstein Avatar answered Oct 19 '22 00:10

Francisco Goldenstein


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)
like image 38
Shimon S Avatar answered Oct 18 '22 23:10

Shimon S


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
        } );
    } );
like image 36
anilam Avatar answered Oct 18 '22 23:10

anilam


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.

like image 21
Nagesh Katke Avatar answered Oct 18 '22 23:10

Nagesh Katke


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>>

enter image description here

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>>

enter image description here

like image 4
Manthan Patel Avatar answered Oct 19 '22 00:10

Manthan Patel