From bc41bd3c61c222c946106e5512eedbd328ec1772 Mon Sep 17 00:00:00 2001 From: Sean Davis Date: Thu, 17 Aug 2023 22:31:32 +0100 Subject: [PATCH] Add datatables order.neutral plugin and use it to allow resetting of sort order on datatables --- bundle.js | 17 +++++++++++++++++ index.html | 1 + scripts/datatables.js | 17 +++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/bundle.js b/bundle.js index ebe17c010..deddc08ec 100644 --- a/bundle.js +++ b/bundle.js @@ -77192,6 +77192,23 @@ const applyDatatables = () => { } } }); + + // Setup a custom handler to reset sort order after descending instead of going back to ascending + // Accomplished by finding and intercepting the mutation of the sorting th's class from desc to asc + const callback = (mutationList) => { + for (const mutation of mutationList) { + if (mutation.type === "attributes" && + mutation.attributeName == "class" && + mutation.oldValue.includes("sorting_desc") && + mutation.target.classList.contains("sorting_asc") + ) { + $(`#${element.id}`).DataTable().order.neutral().draw(); + } + } + }; + document.querySelectorAll('th.sorting').forEach((el) => { + new MutationObserver(callback).observe(el, { attributes: true, attributeFilter: ['class'], attributeOldValue: true }); + }); } catch(e){} }); } diff --git a/index.html b/index.html index 98a6ce8ab..80346c07d 100644 --- a/index.html +++ b/index.html @@ -29,6 +29,7 @@ + diff --git a/scripts/datatables.js b/scripts/datatables.js index 659ffa108..ef682cbea 100644 --- a/scripts/datatables.js +++ b/scripts/datatables.js @@ -39,6 +39,23 @@ const applyDatatables = () => { } } }); + + // Setup a custom handler to reset sort order after descending instead of going back to ascending + // Accomplished by finding and intercepting the mutation of the sorting th's class from desc to asc + const callback = (mutationList) => { + for (const mutation of mutationList) { + if (mutation.type === "attributes" && + mutation.attributeName == "class" && + mutation.oldValue.includes("sorting_desc") && + mutation.target.classList.contains("sorting_asc") + ) { + $(`#${element.id}`).DataTable().order.neutral().draw(); + } + } + }; + document.querySelectorAll('th.sorting').forEach((el) => { + new MutationObserver(callback).observe(el, { attributes: true, attributeFilter: ['class'], attributeOldValue: true }); + }); } catch(e){} }); }