From 1d97da0d3ee979e5f52b4665e10d1ef558602865 Mon Sep 17 00:00:00 2001 From: Andrey Smakotin Date: Tue, 9 Dec 2025 00:59:20 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20TOTAL=5FFORMS=20=D0=BF=D1=80=D0=B8=20?= =?UTF-8?q?=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8=D0=B8=20=D1=84=D0=BE?= =?UTF-8?q?=D1=80=D0=BC=D1=8B=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20=D0=BA?= =?UTF-8?q?=D1=80=D0=B5=D1=81=D1=82=D0=B8=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../orders/templates/orders/order_form.html | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/myproject/orders/templates/orders/order_form.html b/myproject/orders/templates/orders/order_form.html index 4057a3e..b706ffb 100644 --- a/myproject/orders/templates/orders/order_form.html +++ b/myproject/orders/templates/orders/order_form.html @@ -1613,6 +1613,8 @@ document.addEventListener('DOMContentLoaded', function() { const idField = form.querySelector('input[name$="-id"]'); if (idField && idField.value) { + // Сохранённая форма - помечаем на удаление + console.log('[removeForm] Помечаем сохранённую форму на удаление (ID:', idField.value, ')'); deleteCheckbox.checked = true; form.classList.add('deleted'); form.style.display = 'none'; @@ -1621,7 +1623,45 @@ document.addEventListener('DOMContentLoaded', function() { window.orderAutosave.scheduleAutosave(); } } else { + // Новая форма - удаляем из DOM + console.log('[removeForm] Удаление новой формы из DOM'); + + // Удаляем форму form.remove(); + + // Обновляем TOTAL_FORMS + const totalFormsInput = document.querySelector('[name="items-TOTAL_FORMS"]'); + if (totalFormsInput) { + const oldTotal = parseInt(totalFormsInput.value); + const newTotal = oldTotal - 1; + console.log(`[removeForm] Обновление TOTAL_FORMS: ${oldTotal} → ${newTotal}`); + totalFormsInput.value = newTotal; + } + + // Пересчитываем индексы оставшихся форм + const remainingForms = Array.from(document.querySelectorAll('.order-item-form:not(.deleted)')); + console.log(`[removeForm] Пересчёт индексов для ${remainingForms.length} оставшихся форм...`); + + remainingForms.forEach((currentForm, newIndex) => { + // Находим все поля с name="items-N-..." + const fields = currentForm.querySelectorAll('[name^="items-"]'); + fields.forEach(field => { + const name = field.getAttribute('name'); + // Меняем индекс: items-СТАРЫЙ-поле → items-НОВЫЙ-поле + const newName = name.replace(/^items-\d+/, `items-${newIndex}`); + if (name !== newName) { + console.log(`[removeForm] Переименование: ${name} → ${newName}`); + field.setAttribute('name', newName); + + // Обновляем ID тоже (для связи с label) + if (field.id) { + const newId = field.id.replace(/^id_items-\d+/, `id_items-${newIndex}`); + field.setAttribute('id', newId); + } + } + }); + }); + updateTotalDisplay(); } }