Last active
October 2, 2017 17:23
-
-
Save emanoelqueiroz/f65f51697de2f8fbd0f217cf4010e812 to your computer and use it in GitHub Desktop.
Storage sort from DataTable plugin with LocalStorage.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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