Skip to content

Commit 2b25e0d

Browse files
authored
[FEATURE] #179 Add savestate for session and storage, also keep additional GET url-parameter (#297)
1 parent ee493ad commit 2b25e0d

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/Resources/public/js/datatables.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,34 @@
2222
state = ''
2323
;
2424

25+
// Load page state if needed
26+
var stateDuration = null
2527
// Load page state if needed
2628
switch (config.state) {
2729
case 'fragment':
2830
state = window.location.hash;
31+
state = (state.length > 1 ? deparam(state.substr(1)) : {});
2932
break;
3033
case 'query':
3134
state = window.location.search;
35+
state = (state.length > 1 ? deparam(state.substr(1)) : {});
36+
break;
37+
case 'local':
38+
stateDuration = 0
39+
if (localStorage.getItem('DataTables_' + config.name + '_' + window.location.pathname) !== null) {
40+
state = JSON.parse(localStorage.getItem('DataTables_' + config.name + '_' + window.location.pathname))
41+
}
42+
break;
43+
case 'session':
44+
stateDuration = -1
45+
if (sessionStorage.getItem('DataTables_' + config.name + '_' + window.location.pathname) !== null) {
46+
state = JSON.parse(sessionStorage.getItem('DataTables_' + config.name + '_' + window.location.pathname))
47+
}
3248
break;
3349
}
34-
state = (state.length > 1 ? deparam(state.substr(1)) : {});
50+
3551
var persistOptions = config.state === 'none' ? {} : {
52+
stateDuration: stateDuration,
3653
stateSave: true,
3754
stateLoadCallback: function(s, cb) {
3855
// Only need stateSave to expose state() function as loading lazily is not possible otherwise
@@ -63,7 +80,7 @@
6380
var merged = $.extend(true, {}, api.state(), state);
6481

6582
api
66-
.order(merged.order)
83+
.order(state.order ?? api.state().order)
6784
.search(merged.search.search)
6885
.page.len(merged.length)
6986
.page(merged.start / merged.length)
@@ -90,7 +107,6 @@
90107
if (config.state !== 'none') {
91108
dt.on('draw.dt', function(e) {
92109
var data = $.param(dt.state()).split('&');
93-
94110
// First draw establishes state, subsequent draws run diff on the first
95111
if (!baseState) {
96112
baseState = data;
@@ -102,8 +118,13 @@
102118
+ '#' + decodeURIComponent(diff.join('&')));
103119
break;
104120
case 'query':
121+
var windowLocationSearch = deparam(decodeURIComponent(diff.join('&')))
122+
if(window.location.search !== null) {
123+
windowLocationSearch = deparam(window.location.search.substr(1))
124+
Object.assign(windowLocationSearch, deparam(decodeURIComponent(diff.join('&'))))
125+
}
105126
history.replaceState(null, null, window.location.origin + window.location.pathname
106-
+ '?' + decodeURIComponent(diff.join('&') + window.location.hash));
127+
+ '?' + decodeURIComponent($.param(windowLocationSearch) + window.location.hash));
107128
break;
108129
}
109130
}

0 commit comments

Comments
 (0)