Fix: Implement proper deletion of order items with confirmation dialog
Fixes deletion functionality for order items across frontend and backend: - Remove restriction preventing deletion of last item - Add confirmation dialog before deletion - Properly track and send deleted item IDs to backend via autosave - Update backend to handle item deletion by ID instead of index - Fix visual feedback: deleted items are hidden immediately - Auto-recalculate total sum after deletion Technical changes: - order_form.html: Add confirmation dialog, trigger autosave on delete - autosave.js: Collect deleted item IDs, send to backend - draft_service.py: Process deleted_item_ids, update items by ID 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -213,7 +213,7 @@
|
||||
return;
|
||||
}
|
||||
|
||||
const fields = form.querySelectorAll('select, input[type="number"], input[type="checkbox"]');
|
||||
const fields = form.querySelectorAll('select, input[type="number"], input[type="text"], input[type="checkbox"]');
|
||||
|
||||
fields.forEach(field => {
|
||||
if (field.tagName === 'SELECT' || field.type === 'checkbox') {
|
||||
@@ -421,7 +421,9 @@
|
||||
}
|
||||
|
||||
// Собираем позиции заказа
|
||||
data.items = collectOrderItems();
|
||||
const orderItemsData = collectOrderItems();
|
||||
data.items = orderItemsData.items;
|
||||
data.deleted_item_ids = orderItemsData.deletedItemIds;
|
||||
|
||||
// Флаг для пересчета итоговой суммы
|
||||
data.recalculate = true;
|
||||
@@ -434,13 +436,20 @@
|
||||
*/
|
||||
function collectOrderItems() {
|
||||
const items = [];
|
||||
const deletedItemIds = [];
|
||||
const itemForms = document.querySelectorAll('.order-item-form');
|
||||
|
||||
itemForms.forEach(form => {
|
||||
// Пропускаем удаленные формы
|
||||
// Проверяем, помечена ли форма на удаление
|
||||
const deleteCheckbox = form.querySelector('input[name$="-DELETE"]');
|
||||
const idField = form.querySelector('input[name$="-id"]');
|
||||
|
||||
if (deleteCheckbox && deleteCheckbox.checked) {
|
||||
return;
|
||||
// Если форма помечена на удаление и имеет ID, добавляем в список удалённых
|
||||
if (idField && idField.value) {
|
||||
deletedItemIds.push(parseInt(idField.value));
|
||||
}
|
||||
return; // Не добавляем в items
|
||||
}
|
||||
|
||||
// Получаем выбранный товар/комплект
|
||||
@@ -459,9 +468,14 @@
|
||||
|
||||
const item = {
|
||||
quantity: quantityInput.value || '1',
|
||||
price: priceInput.value || '0'
|
||||
price: (priceInput.value || '0').replace(',', '.')
|
||||
};
|
||||
|
||||
// Если есть ID (существующий товар), добавляем его
|
||||
if (idField && idField.value) {
|
||||
item.id = parseInt(idField.value);
|
||||
}
|
||||
|
||||
// Определяем тип: товар или комплект
|
||||
if (itemValue.startsWith('product_')) {
|
||||
item.product_id = parseInt(itemValue.replace('product_', ''));
|
||||
@@ -472,7 +486,7 @@
|
||||
items.push(item);
|
||||
});
|
||||
|
||||
return items;
|
||||
return { items, deletedItemIds };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -544,4 +558,9 @@
|
||||
init();
|
||||
}
|
||||
|
||||
// Экспортируем функцию scheduleAutosave в глобальную область
|
||||
window.orderAutosave = {
|
||||
scheduleAutosave: scheduleAutosave
|
||||
};
|
||||
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user