From 7e9d4450514fa46ad6c8c631cfe08c8237686f06 Mon Sep 17 00:00:00 2001 From: Verox001 Date: Wed, 28 Feb 2024 10:57:48 +0100 Subject: [PATCH] Fixed multiple list sorting --- templates/components/scripts.html | 44 +++++++++++++++++-------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/templates/components/scripts.html b/templates/components/scripts.html index f7ae68b..02d18ab 100644 --- a/templates/components/scripts.html +++ b/templates/components/scripts.html @@ -66,27 +66,31 @@ document.addEventListener('DOMContentLoaded', (event) => { - var el = document.querySelector('.task-list'); - Sortable.create(el, { - handle: '.drag-handle', - animation: 150, - onEnd: function (evt) { - var habitId = el.children[evt.newIndex].id.split('-')[1]; - var oldIndex = evt.oldIndex; - var newIndex = evt.newIndex; + var elements = document.querySelectorAll('.task-list').values() - axios.post('/reorder', {habitId: habitId, oldIndex: oldIndex, newIndex: newIndex}, { - headers: { - 'Content-Type': 'application/json' - } - }).then(function (response) { - // Handle the success response if needed - }).catch(function (error) { - // Handle the error if needed - console.error('Error:', error); - }); - } - }); + // loop through the elements + for (let el of elements) { + Sortable.create(el, { + handle: '.drag-handle', + animation: 150, + onEnd: function (evt) { + var habitId = el.children[evt.newIndex].id.split('-')[1]; + var oldIndex = evt.oldIndex; + var newIndex = evt.newIndex; + + axios.post('/reorder', {habitId: habitId, oldIndex: oldIndex, newIndex: newIndex}, { + headers: { + 'Content-Type': 'application/json' + } + }).then(function (response) { + // Handle the success response if needed + }).catch(function (error) { + // Handle the error if needed + console.error('Error:', error); + }); + } + }); + } });