The problem I'm experiencing in Chrome and Edge:
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.
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.
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.
There is a option called pageLength . You can set this for show only 5 entries.
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();
});
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:
Scenario 2: Users copies URL after state save has taken place
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With