From a922f8acf1dcb32291ab62c85b7bcebdd8d64c90 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 19 Jan 2021 14:16:13 +0100 Subject: [PATCH] Bug 27467: [SOLUTION 3] Use stateSave Based on https://stackoverflow.com/questions/55446923/datatables-1-10-using-savestate-to-remember-filtering-and-order-but-need-to-upd/60708638#60708638 (I removed the atob and btoa call) This solution is finally working but it's not the ideal one IMO. Using stateSave will modify the URL when the user modifies the table's settings. We would prefer to not modify it but have a "Copy link" buttons on top of the table instead. --- koha-tmpl/intranet-tmpl/prog/js/datatables.js | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/koha-tmpl/intranet-tmpl/prog/js/datatables.js b/koha-tmpl/intranet-tmpl/prog/js/datatables.js index 6baa84b8bb..826f14d659 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/datatables.js +++ b/koha-tmpl/intranet-tmpl/prog/js/datatables.js @@ -63,6 +63,33 @@ var dataTablesDefaults = { $("#" + tableId + "_wrapper").find(".dt_button_clear_filter").removeClass("disabled"); } }); + }, + stateSave: true, + stateSaveCallback: function (settings, data) { + const state = JSON.stringify(data); + //get query part of the url + let searchParams = new URLSearchParams(window.location.search); + //add encoded state into query part + searchParams.set($(this).attr('id') + '_state', state); + //form url with new query parameter + const newRelativePathQuery = window.location.pathname + '?' + searchParams.toString() + window.location.hash; + //push new url into history object, this will change the current url without need of reload + history.pushState(null, '', newRelativePathQuery); + }, + stateLoadCallback: function (settings) { + const url = new URL(window.location.href); + let state = url.searchParams.get($(this).attr('id') + '_state'); + + //check the current url to see if we've got a state to restore + if (!state) { + return null; + } + + //if we got the state, add current timestamp + state = JSON.parse(state); + state['time'] = Date.now(); + + return state; } }; -- 2.20.1