|
22 | 22 | state = '' |
23 | 23 | ; |
24 | 24 |
|
| 25 | + // Load page state if needed |
| 26 | + var stateDuration = null |
25 | 27 | // Load page state if needed |
26 | 28 | switch (config.state) { |
27 | 29 | case 'fragment': |
28 | 30 | state = window.location.hash; |
| 31 | + state = (state.length > 1 ? deparam(state.substr(1)) : {}); |
29 | 32 | break; |
30 | 33 | case 'query': |
31 | 34 | 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 | + } |
32 | 48 | break; |
33 | 49 | } |
34 | | - state = (state.length > 1 ? deparam(state.substr(1)) : {}); |
| 50 | + |
35 | 51 | var persistOptions = config.state === 'none' ? {} : { |
| 52 | + stateDuration: stateDuration, |
36 | 53 | stateSave: true, |
37 | 54 | stateLoadCallback: function(s, cb) { |
38 | 55 | // Only need stateSave to expose state() function as loading lazily is not possible otherwise |
|
63 | 80 | var merged = $.extend(true, {}, api.state(), state); |
64 | 81 |
|
65 | 82 | api |
66 | | - .order(merged.order) |
| 83 | + .order(state.order ?? api.state().order) |
67 | 84 | .search(merged.search.search) |
68 | 85 | .page.len(merged.length) |
69 | 86 | .page(merged.start / merged.length) |
|
90 | 107 | if (config.state !== 'none') { |
91 | 108 | dt.on('draw.dt', function(e) { |
92 | 109 | var data = $.param(dt.state()).split('&'); |
93 | | - |
94 | 110 | // First draw establishes state, subsequent draws run diff on the first |
95 | 111 | if (!baseState) { |
96 | 112 | baseState = data; |
|
102 | 118 | + '#' + decodeURIComponent(diff.join('&'))); |
103 | 119 | break; |
104 | 120 | 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 | + } |
105 | 126 | history.replaceState(null, null, window.location.origin + window.location.pathname |
106 | | - + '?' + decodeURIComponent(diff.join('&') + window.location.hash)); |
| 127 | + + '?' + decodeURIComponent($.param(windowLocationSearch) + window.location.hash)); |
107 | 128 | break; |
108 | 129 | } |
109 | 130 | } |
|
0 commit comments