Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting DataTables to keep its state when the user clicks the back button (without the stateSave option)

The problem I'm experiencing in Chrome and Edge:

  1. Go to https://datatables.net/examples/basic_init/zero_configuration.html
  2. Sort the table by some column (e.g. "Age")
  3. Use the pagination interface at the bottom of the table to go to one of the other pages
  4. Click on one of the navigation links to the left (e.g. "FAQs" or "Download")
  5. Click the browser's back button and observe that the table is now back to its original state (sorted by the "Name" column and on page 1)

In Firefox, the table is still sorted by the correct column and is still on the correct page. How can I make Chrome and Edge also behave this way?

I know DataTables has its stateSave option (documentation and example), but the problem with that is when the user navigates around the site and then clicks a link to go to the page that has the DataTables table, it will put them back into the same state in that scenario too. I only want the user to be put back into the same state if they use their browser's back button.

like image 889
Nick Avatar asked Nov 11 '16 15:11

Nick


People also ask

What is stateSave in DataTables?

When DataTables' state saving option is enabled ( stateSave ) KeyTable will automatically store the last cell focused in a table and then restore that state when the page is reloaded.

What does dataTable destroy do?

function destroy( [ remove ] ) Description: Restore the tables in the current context to its original state in the DOM by removing all of DataTables enhancements, alterations to the DOM structure of the table and event listeners.

How do I show only 5 entries in DataTables?

There is a option called pageLength . You can set this for show only 5 entries.


2 Answers

Based on this post you could clear the saved state when you click on the link that leads you to the page with the table

see example here

$(document).ready(function() {
    $('#example').DataTable( {
        "paging":   true,
        "ordering": true,
        "info":     false,
        stateSave: true
    } );
} );

$(".table_link").on("click", function(){
  $('#example').DataTable().state.clear();
});
like image 187
Dex Dave Avatar answered Sep 24 '22 08:09

Dex Dave


Ok have kind of a crazy idea that might work for this. If you use the "stateSaveCallback" to set a URL hash this will add an item into the browser history. Then you could check for the hash value when the page loads. If the hash is not present then you clear the state cache on the DataTable.

Where this breaks down is in the following scenarios:

Scenario 1: User presses back button after state save on data table page:

  • User does something on grid.
  • State is saved triggering the stateSaveCallback
  • stateSaveCallback updates the "window.location.hash" value.
  • User then presses the "back" button
  • Page goes to the current URL except without the hash.
  • State is cleared.

Scenario 2: Users copies URL after state save has taken place

  • User does something on grid.
  • State is saved triggering the stateSaveCallback
  • stateSaveCallback updates the "window.location.hash" value.
  • User manually copies URL which includes hash value.
  • User uses this copied value to directly navigate to data table page.
  • Previous state won't be cleared.

But in all the other scenarios provided as long as you don't include the hash code on your navigation links this would reliably detect if a user used the back button to navigate to the grid since it would be it's own history item.

like image 25
Adrian Avatar answered Sep 26 '22 08:09

Adrian