20

I have a datable and "Edit" button in each row. When I click that button, /edit/ url opens. Everything is Ok until now. But if I need to go back to the datatable, it starts from the first page. What can I do for that?

 $('#table').dataTable({
        "sDom" : 'rtFip>',
        'fnDrawCallback' : function() {
            $('input:checkbox, input:radio').checkbox();
        },
        'sPaginationType' : 'full_numbers',
        "bServerSide" : true,
        "sAjaxSource" : "{% url 'get_menu_list' %}"
  });
3
  • 1
    what a vague question. should we use our imagination?
    – Ram
    Commented Jul 9, 2012 at 18:00
  • Post the code of the button and the dataTable initialization. Have you also tried asking in the dataTables forums? datatables.net/forums Commented Jul 9, 2012 at 18:15
  • Yes, I asked there also.
    – Burak
    Commented Jul 9, 2012 at 18:17

4 Answers 4

44

DataTables has option to store state in a cookie.

$(document).ready(function() {
    $('#example').dataTable( {
        "stateSave": true
    } );
} );

http://datatables.net/examples/basic_init/state_save.html

4
  • Yep, no luck in Chrome. Does anybody know if this has been fixed in a later version? I'm on 1.10.9.
    – LandonC
    Commented Nov 4, 2015 at 19:55
  • Maybe it has to do with the version of Chrome you are using. I'm using Chrome Version 45.0.2454.93 (64-bit) on LInux openSUSE 13.2, and it's working. Though I've not tried it with dataTables version 1.10.9; I'm still using dataTable version 1.10.7
    – mtchuente
    Commented Nov 6, 2015 at 14:53
  • And what if i want to return to specific row on page? how is it possible Commented Apr 13, 2016 at 9:13
  • Great! but that's not all folks, this will store data in localstorage, if you want it to be in session storage or DB here is some more info from this other user stackoverflow.com/a/38442163/2914407 for me localstorage messed things up in reactjs Commented Apr 3, 2019 at 14:57
5

Storing the state in a cookie fails for large tables. This local storage solution works for large tables.

$(document).ready(function() {
    $('#example').dataTable( {
        "bStateSave": true,
        "fnStateSave": function (oSettings, oData) {
            localStorage.setItem( 'DataTables', JSON.stringify(oData) );
        },
        "fnStateLoad": function (oSettings) {
            return JSON.parse( localStorage.getItem('DataTables') );
        }
    } );
} );

Source: http://datatables.net/blog/localStorage_for_state_saving

4

How to return the specific page in jQuery Datatables paging?

Use fnPagingInfo

Get information about the paging settings that DataTables is currently using to display each page, including the number of records shown, start and end points in the data set etc.

      $.fn.dataTableExt.oApi.fnPagingInfo = function ( oSettings )
      {
        return {
          "iStart":         oSettings._iDisplayStart,
          "iEnd":           oSettings.fnDisplayEnd(),
          "iLength":        oSettings._iDisplayLength,
          "iTotal":         oSettings.fnRecordsTotal(),
          "iFilteredTotal": oSettings.fnRecordsDisplay(),
          "iPage":          Math.ceil( oSettings._iDisplayStart / oSettings._iDisplayLength ),
          "iTotalPages":    Math.ceil( oSettings.fnRecordsDisplay() / oSettings._iDisplayLength )
        };
      };


      $(document).ready(function() {
      $('#example').dataTable( {
      "fnDrawCallback": function () {
      alert( 'Now on page'+ this.fnPagingInfo().iPage );
      }
      } );
      } );

Source: http://datatables.net/plug-ins/api

2

Here is how to do it in v1.10.7:

$(document).ready(function() {
    $('#example').dataTable( {
        stateSave: true
     } );
} );

Not the answer you're looking for? Browse other questions tagged or ask your own question.