Carpeta js
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
// Guarda referencia al form original (si existe)
|
||||
|
||||
let formConfirmacion = null;
|
||||
|
||||
// Acción dinámica a ejecutar (opt del backend)
|
||||
|
||||
let action = null;
|
||||
|
||||
// Objeto donde se copian todos los data-* del botón
|
||||
|
||||
let dataset = {};
|
||||
|
||||
// Recorre todos los botones que disparan el modal
|
||||
|
||||
document.querySelectorAll(".btnConfirmar").forEach(boton => {
|
||||
|
||||
boton.addEventListener("click", function () {
|
||||
|
||||
// Inserta el mensaje en el modal
|
||||
|
||||
document.getElementById("modalMensaje").textContent = this.dataset.mensaje;
|
||||
|
||||
// Inserta el título en el modal
|
||||
|
||||
document.getElementById("modalTitulo").textContent = this.dataset.titulo;
|
||||
|
||||
// Busca si el botón pertenece a un form existente
|
||||
|
||||
formConfirmacion = this.closest("form");
|
||||
|
||||
// Obtiene la acción dinámica (si existe)
|
||||
|
||||
action = this.dataset.action || null;
|
||||
|
||||
// Copia TODOS los data-* del botón a un objeto JS
|
||||
|
||||
dataset = { ...this.dataset };
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
// Evento del botón "Confirmar" dentro del modal
|
||||
|
||||
document.getElementById("modalBtnConfirmar").addEventListener("click", function (e) {
|
||||
|
||||
// Evita el submit automático del botón
|
||||
|
||||
e.preventDefault();
|
||||
|
||||
// CASO 1: FORM EXISTENTE
|
||||
|
||||
// Si hay un form padre y no hay acción dinámica
|
||||
|
||||
if (formConfirmacion && !action) {
|
||||
|
||||
// Envía directamente ese form
|
||||
|
||||
formConfirmacion.submit();
|
||||
return;
|
||||
}
|
||||
|
||||
// CASO 2: FORM DINÁMICO
|
||||
|
||||
if (action) {
|
||||
|
||||
// Form global del modal
|
||||
|
||||
const form = document.getElementById("formConfirmacionGlobal");
|
||||
|
||||
// Contenedor donde se insertan inputs ocultos
|
||||
|
||||
const container = document.getElementById("modalDynamicInputs");
|
||||
|
||||
// Define la URL de envío con el parámetro opt
|
||||
|
||||
form.action = `index.php?opt=${action}`;
|
||||
|
||||
// Limpia inputs anteriores para evitar duplicados
|
||||
|
||||
container.innerHTML = '';
|
||||
|
||||
// Recorre todas las claves del dataset
|
||||
|
||||
Object.keys(dataset).forEach(key => {
|
||||
|
||||
// Ignora atributos que no deben enviarse
|
||||
|
||||
if ([
|
||||
'action',
|
||||
'mensaje',
|
||||
'titulo',
|
||||
'bsToggle',
|
||||
'bsTarget'
|
||||
].includes(key)) return;
|
||||
|
||||
// Crea un input hidden por cada data-*
|
||||
|
||||
const input = document.createElement('input');
|
||||
|
||||
input.type = 'hidden'; // oculto
|
||||
input.name = key; // nombre (ej: mano_obra, etc)
|
||||
input.value = dataset[key]; // valor correspondiente
|
||||
|
||||
// Lo agrega al form
|
||||
|
||||
container.appendChild(input);
|
||||
});
|
||||
|
||||
// Envía el form dinámico
|
||||
|
||||
form.submit();
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user