Исправлено обновление TOTAL_FORMS при удалении формы через крестик
This commit is contained in:
@@ -1613,6 +1613,8 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||||||
const idField = form.querySelector('input[name$="-id"]');
|
const idField = form.querySelector('input[name$="-id"]');
|
||||||
|
|
||||||
if (idField && idField.value) {
|
if (idField && idField.value) {
|
||||||
|
// Сохранённая форма - помечаем на удаление
|
||||||
|
console.log('[removeForm] Помечаем сохранённую форму на удаление (ID:', idField.value, ')');
|
||||||
deleteCheckbox.checked = true;
|
deleteCheckbox.checked = true;
|
||||||
form.classList.add('deleted');
|
form.classList.add('deleted');
|
||||||
form.style.display = 'none';
|
form.style.display = 'none';
|
||||||
@@ -1621,7 +1623,45 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||||||
window.orderAutosave.scheduleAutosave();
|
window.orderAutosave.scheduleAutosave();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// Новая форма - удаляем из DOM
|
||||||
|
console.log('[removeForm] Удаление новой формы из DOM');
|
||||||
|
|
||||||
|
// Удаляем форму
|
||||||
form.remove();
|
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();
|
updateTotalDisplay();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user