Use the column order stored in localStorage during the initial datatables ajax call.

This way we still have the right ordering when changing pages. This fixes issue #345
This commit is contained in:
Jan Böhmer 2023-08-20 00:41:44 +02:00
parent 73d61f7440
commit e66ff40733
2 changed files with 22 additions and 3 deletions

View file

@ -65,8 +65,13 @@ export default class extends Controller {
localStorage.setItem( this.getStateSaveKey(), JSON.stringify(data) );
}
stateLoadCallback(settings) {
const data = JSON.parse( localStorage.getItem(this.getStateSaveKey()) );
stateLoadCallback() {
const json = localStorage.getItem(this.getStateSaveKey());
if(json === null || json === undefined) {
return null;
}
const data = JSON.parse(json);
if (data) {
//Do not save the start value (current page), as we want to always start at the first page on a page reload
@ -90,6 +95,19 @@ export default class extends Controller {
//Add url info, as the one available in the history is not enough, as Turbo may have not changed it yet
settings.url = this.element.dataset.dtUrl;
//Add initial_order info to the settings, so that the order on the initial page load is the one saved in the state
const saved_state = this.stateLoadCallback();
if (saved_state !== null) {
const raw_order = saved_state.order;
settings.initial_order = raw_order.map((order) => {
return {
column: order[0],
dir: order[1]
}
});
}
let options = {
colReorder: true,
responsive: true,