state.clear() is not working

state.clear() is not working

manojsitaparamanojsitapara Posts: 3Questions: 1Answers: 0

I am using datatables.net version 1.10.4.
I am trying to clear state on page load event. Here is my code on which I am trying to clear state, but it is not working. Can anyone help to resolve this?

var invoiceTable = $('#invoiceDetails').dataTable({
"paging": true,
"stateSave": true, //to save pagination state
"ordering": true,
//"iCookieDuration": 1,
"info": false,
"searching": false,
"lengthChange": false,
"pageLength": 5,
"columnDefs": [
{
"targets": [0, 10],
"orderable": false
}
]
});
var datable = $('#invoiceDetails').DataTable();
datable .state.clear();

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,822Questions: 1Answers: 10,127 Site admin

    Problem there is that the table has already loaded the saved state. If you don't want it to load the saved state, remove the stateSave: true.

    Allan

  • manojsitaparamanojsitapara Posts: 3Questions: 1Answers: 0

    Thank you allan for your answer.

    I want to save state but when page is gets reload, it should be remove save state.

    I want to keep save state true because I have drop down in my last column and when user click on it, it is opening modal pop up & user save data. once modal pop up gets close, datatable state should be saved & user needs to stay on that table page(say page number 3 in table pagination).

    I want to remove save state when page reload or user will go to some page and comes back to same page, state should be clear.

  • allanallan Posts: 61,822Questions: 1Answers: 10,127 Site admin
    Answer ✓

    once modal pop up gets close, datatable state should be saved & user needs to stay on that table page(say page number 3 in table pagination).

    If the modal is a Javascript widget, then you presumably aren't destroying the table and therefore it will already still be on page 3 (for example)? As such you wouldn't need state saving.

    Allan

  • manojsitaparamanojsitapara Posts: 3Questions: 1Answers: 0

    Hi Allan,

    I found the solution. I did following steps.

    1. clear state
      var table = $('#invoiceDetails').DataTable();
      table.state.clear();
    2. destroy table
      table.destroy();
    3. again Initialize Invoice table
      $('#invoiceDetails').dataTable({
      "paging": true,
      "stateSave": true, //to save pagination state
      "ordering": true,
      //"iCookieDuration": 1,
      "info": false,
      "searching": false,
      "lengthChange": false,
      "pageLength": 5,
      "columnDefs": [
      {
      "targets": [0, 10],
      "orderable": false
      }
      ]
      });

    Once again thank you for your help.

This discussion has been closed.