Permitir editar el texto/cantidad/precio de un articulo ya anadido (boton editar en la lista)
This commit is contained in:
@@ -262,6 +262,7 @@
|
|||||||
let currentItems = [];
|
let currentItems = [];
|
||||||
let allBudgets = [];
|
let allBudgets = [];
|
||||||
let editingId = null; // null = modo crear, con valor = modo editar
|
let editingId = null; // null = modo crear, con valor = modo editar
|
||||||
|
let editingItemIndex = null; // null = añadiendo nuevo articulo, con valor = editando uno existente
|
||||||
|
|
||||||
// ------------------ INICIALIZACIÓN Y TEMA CORPORATIVO ------------------
|
// ------------------ INICIALIZACIÓN Y TEMA CORPORATIVO ------------------
|
||||||
async function applyTheme() {
|
async function applyTheme() {
|
||||||
@@ -297,6 +298,7 @@
|
|||||||
// ------------------ NAVEGACIÓN Y TOASTS ------------------
|
// ------------------ NAVEGACIÓN Y TOASTS ------------------
|
||||||
function showCreateView() {
|
function showCreateView() {
|
||||||
editingId = null;
|
editingId = null;
|
||||||
|
editingItemIndex = null;
|
||||||
document.getElementById('createViewTitle').textContent = 'Generar Nuevo';
|
document.getElementById('createViewTitle').textContent = 'Generar Nuevo';
|
||||||
document.getElementById('btnSave').innerHTML = '<i data-lucide="save" class="w-4 h-4"></i> Generar Presupuesto';
|
document.getElementById('btnSave').innerHTML = '<i data-lucide="save" class="w-4 h-4"></i> Generar Presupuesto';
|
||||||
document.getElementById('viewList').classList.add('hidden');
|
document.getElementById('viewList').classList.add('hidden');
|
||||||
@@ -315,6 +317,7 @@
|
|||||||
const budget = allBudgets.find(b => b.id === id);
|
const budget = allBudgets.find(b => b.id === id);
|
||||||
if (!budget) return showToast("Error: Presupuesto no encontrado");
|
if (!budget) return showToast("Error: Presupuesto no encontrado");
|
||||||
editingId = id;
|
editingId = id;
|
||||||
|
editingItemIndex = null;
|
||||||
document.getElementById('c_name').value = budget.client_name || '';
|
document.getElementById('c_name').value = budget.client_name || '';
|
||||||
document.getElementById('c_phone').value = budget.client_phone || '';
|
document.getElementById('c_phone').value = budget.client_phone || '';
|
||||||
document.getElementById('c_address').value = budget.client_address || '';
|
document.getElementById('c_address').value = budget.client_address || '';
|
||||||
@@ -635,7 +638,12 @@
|
|||||||
|
|
||||||
if(!concept || isNaN(qty) || isNaN(price)) { showToast("Rellena concepto y precio."); return; }
|
if(!concept || isNaN(qty) || isNaN(price)) { showToast("Rellena concepto y precio."); return; }
|
||||||
|
|
||||||
|
if (editingItemIndex !== null) {
|
||||||
|
currentItems[editingItemIndex] = { concept, qty, price, total: qty * price };
|
||||||
|
editingItemIndex = null;
|
||||||
|
} else {
|
||||||
currentItems.push({ concept, qty, price, total: qty * price });
|
currentItems.push({ concept, qty, price, total: qty * price });
|
||||||
|
}
|
||||||
conceptInput.value = ''; qtyInput.value = '1'; priceInput.value = '';
|
conceptInput.value = ''; qtyInput.value = '1'; priceInput.value = '';
|
||||||
conceptInput.focus();
|
conceptInput.focus();
|
||||||
renderItems();
|
renderItems();
|
||||||
@@ -646,6 +654,17 @@
|
|||||||
renderItems();
|
renderItems();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function editItem(idx) {
|
||||||
|
const item = currentItems[idx];
|
||||||
|
if (!item) return;
|
||||||
|
document.getElementById('item_concept').value = item.concept;
|
||||||
|
document.getElementById('item_qty').value = item.qty;
|
||||||
|
document.getElementById('item_price').value = item.price;
|
||||||
|
editingItemIndex = idx;
|
||||||
|
document.getElementById('item_concept').focus();
|
||||||
|
showToast("Modifica los datos y pulsa + para guardar los cambios");
|
||||||
|
}
|
||||||
|
|
||||||
function renderItems() {
|
function renderItems() {
|
||||||
const cont = document.getElementById('itemsContainer');
|
const cont = document.getElementById('itemsContainer');
|
||||||
cont.innerHTML = currentItems.map((item, idx) => `
|
cont.innerHTML = currentItems.map((item, idx) => `
|
||||||
@@ -656,6 +675,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="flex items-center gap-3">
|
<div class="flex items-center gap-3">
|
||||||
<span class="font-black text-slate-800">${item.total.toFixed(2)}€</span>
|
<span class="font-black text-slate-800">${item.total.toFixed(2)}€</span>
|
||||||
|
<button type="button" onclick="editItem(${idx})" class="w-7 h-7 bg-white shadow-sm border border-slate-100 text-blue-500 rounded-full flex items-center justify-center active:scale-90"><i data-lucide="pencil" class="w-4 h-4"></i></button>
|
||||||
<button type="button" onclick="removeItem(${idx})" class="w-7 h-7 bg-white shadow-sm border border-slate-100 text-rose-500 rounded-full flex items-center justify-center active:scale-90"><i data-lucide="trash-2" class="w-4 h-4"></i></button>
|
<button type="button" onclick="removeItem(${idx})" class="w-7 h-7 bg-white shadow-sm border border-slate-100 text-rose-500 rounded-full flex items-center justify-center active:scale-90"><i data-lucide="trash-2" class="w-4 h-4"></i></button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user