-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
56 lines (44 loc) · 2.09 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Función para actualizar el total
function actualizarTotal() {
var row = this.parentNode.parentNode; // Obtener la fila actual
var precio = parseFloat(row.children[1].textContent); // Obtener el precio de la segunda columna
var cantidad = parseFloat(this.value); // Obtener la cantidad ingresada en el input
var total = precio * cantidad; // Calcular el total
row.children[3].textContent = total.toFixed(2); // Mostrar el total en la cuarta columna
}
window.addEventListener('DOMContentLoaded', function() {
// Obtener todos los elementos de entrada de cantidad
var quantityInputs = document.querySelectorAll('#productTable input[type="number"]');
// Asociar evento de cambio a cada elemento de entrada de cantidad
quantityInputs.forEach(function(input) {
input.addEventListener('change', actualizarTotal);
});
// Obtener el botón "Limpiar vacíos"
var limpiarBtn = document.getElementById('limpiar');
// Asociar evento de clic al botón
limpiarBtn.addEventListener('click', limpiarVacios);
// Función para limpiar campos vacíos
function limpiarVacios() {
var table = document.getElementById('productTable');
var rows = table.querySelectorAll('tr');
var total = 0;
rows.forEach(function(row) {
var input = row.querySelector('input[type="number"]');
if (input && input.value === '') {
row.remove(); // Eliminar la fila si el input está vacío
} else {
var subtotal = parseFloat(row.children[3].textContent);
if (!isNaN(subtotal)) {
total += subtotal; // Sumar el subtotal al total
}
}
});
// Crear una nueva fila para mostrar el total a pagar
var totalRow = document.createElement('tr');
var totalCell = document.createElement('td');
totalCell.setAttribute('colspan', '4');
totalCell.textContent = 'Total a pagar: ' + total.toFixed(2);
totalRow.appendChild(totalCell);
table.appendChild(totalRow);
}
});