Skip to content

Commit 3a9e31e

Browse files
committed
Make tables fully stateful via fragment/query
Closes #3
1 parent 1c6ff2e commit 3a9e31e

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

src/Resources/assets/js/datatables.js

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@
3030
state = (state.length > 1 ? deparam(state.substr(1)) : {});
3131
var persistOptions = config.state === 'none' ? {} : {
3232
stateSave: true,
33-
stateLoadCallback: function(settings) {
34-
var copy = JSON.parse(JSON.stringify(state));
35-
copy.time = Date.now();
36-
return copy;
33+
stateLoadCallback: function(s, cb) {
34+
// Only need stateSave to expose state() function as loading lazily is not possible otherwise
35+
return null;
3736
}
3837
};
3938

@@ -46,14 +45,24 @@
4645
_init: true
4746
}
4847
}).done(function(data) {
49-
var rebuild = (Object.keys(state).length === 0), cached, baseState;
48+
var baseState;
5049

50+
// Merge all options from different sources together and add the Ajax loader
5151
var dtOpts = $.extend({}, data.options, config.options, options, persistOptions, {
5252
ajax: function (request, drawCallback, settings) {
53-
if (rebuild) {
53+
if (data) {
5454
data.draw = request.draw;
5555
drawCallback(data);
56-
rebuild = false;
56+
data = null;
57+
if (Object.keys(state).length) {
58+
var merged = $.extend(true, {}, dt.state(), state);
59+
dt
60+
.order(merged.order)
61+
.search(merged.search.search)
62+
.page.len(merged.length)
63+
.page(merged.start / merged.length)
64+
.draw(false);
65+
}
5766
} else {
5867
request._dt = config.name;
5968
$.ajax(config.url, {
@@ -69,20 +78,24 @@
6978
root.html(data.template);
7079
dt = $('table', root).DataTable(dtOpts);
7180
if (config.state !== 'none') {
72-
dt.on('stateSaveParams.dt', (e, settings, data) => {
73-
//$.extend(data, $('form[name={{ filterForm.vars.name }}]').serializeObject());
74-
data = $.param(data).split('&');
75-
baseState = baseState || data;
76-
var diff = data.filter(el => { return baseState.indexOf(el) === -1 && el.indexOf('time=') !== 0; });
77-
switch (config.state) {
78-
case 'fragment':
79-
history.replaceState(null, null, '#' + decodeURIComponent(diff.join('&')));
80-
break;
81-
case 'query':
82-
history.replaceState(null, null, '#' + decodeURIComponent(diff.join('&')));
83-
break;
81+
dt.on('draw.dt', function(e) {
82+
var data = $.param(dt.state()).split('&');
83+
84+
// First draw establishes state, subsequent draws run diff on the first
85+
if (!baseState) {
86+
baseState = data;
87+
} else {
88+
var diff = data.filter(el => { return baseState.indexOf(el) === -1 && el.indexOf('time=') !== 0; });
89+
switch (config.state) {
90+
case 'fragment':
91+
history.replaceState(null, null, '#' + decodeURIComponent(diff.join('&')));
92+
break;
93+
case 'query':
94+
history.replaceState(null, null, '?' + decodeURIComponent(diff.join('&')));
95+
break;
96+
}
8497
}
85-
});
98+
})
8699
}
87100

88101
fulfill(dt);

0 commit comments

Comments
 (0)