Skip to content

Instantly share code, notes, and snippets.

@emanoelqueiroz
Last active October 2, 2017 17:23
Show Gist options
  • Save emanoelqueiroz/f65f51697de2f8fbd0f217cf4010e812 to your computer and use it in GitHub Desktop.
Save emanoelqueiroz/f65f51697de2f8fbd0f217cf4010e812 to your computer and use it in GitHub Desktop.
Storage sort from DataTable plugin with LocalStorage.
// Just puting the table element on a variable
// to not search for the element every time;
let $dataTable = $('#table');
// Get the actual order;
$dataTable.DataTable().order();
// Insert new order;
$dataTable.DataTable().order(/* params... */);
// When event is called;
let localStorageObj = JSON.parse(localStorage.getItem('tableOrder') || '{}');
$dataTable.on('order.dt', () => {
// Put the order in the localStorage using the actual URL;
localStorageObj[window.location.href] = $dataTable.DataTable().order()[0];
localStorage.setItem('tableOrder', JSON.stringify(localStorageObj));
});
// Set the order from localStorage to the table order;
if (localStorageObj[window.location.href]) {
$dataTable.DataTable().order(localStorageObj[window.location.href]).draw();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment