Skip to content

Commit 7f16f7a

Browse files
committed
Save re-ordering
1 parent 473e712 commit 7f16f7a

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

resources/views/filament/resources/menu-resource/pages/manage-menu-items.blade.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,24 +92,35 @@ function menuItemsManager(config) {
9292
group: 'menu-items',
9393
animation: 150,
9494
handle: '.drag-handle',
95+
onStart: (evt) => {
96+
// Store the original order and DOM state before drag
97+
this.originalOrder = Array.from(container.children).map(el => this.extractItemId(el));
98+
this.originalElements = Array.from(container.children);
99+
},
95100
onEnd: (evt) => {
96101
if (evt.oldIndex !== evt.newIndex) {
97-
this.saveReorder(evt);
102+
// Calculate the new order based on the drag operation
103+
const newOrder = [...this.originalOrder];
104+
const movedItem = newOrder.splice(evt.oldIndex, 1)[0];
105+
newOrder.splice(evt.newIndex, 0, movedItem);
106+
107+
// Immediately revert DOM to original state to preserve Livewire
108+
container.innerHTML = '';
109+
this.originalElements.forEach(el => container.appendChild(el));
110+
111+
this.saveReorderFromOrder(newOrder);
98112
}
99113
}
100114
});
101115
}
102116
}
103117
},
104118
105-
saveReorder(evt) {
119+
saveReorderFromOrder(newOrder) {
106120
this.loading = true;
107121
108-
// Get all items in their new order
109-
const container = document.getElementById('menu-items-container');
110-
const items = Array.from(container.children).map((element, index) => {
111-
// Extract item ID from the wire:key attribute or data attribute
112-
const itemId = this.extractItemId(element);
122+
// Build items array from the calculated new order
123+
const items = newOrder.map((itemId, index) => {
113124
return {
114125
id: itemId,
115126
position: index,

0 commit comments

Comments
 (0)