What I need is the following:
I generate a table with data that I get from the BDD
and I do the calculations, this is not the problem, it is already solved, the thing is that I need a input
where I can enter a value (interest) and this value must be calculated with the table already generated or as it can.
When I enter another value in the input
console I am showing that data to see which ones cause me problems, and the error is with the data called from the BDD
.
JS code;
function Generar_Datos_depr(datos) {
var anios_presupuesto = parseFloat(document.getElementById('años_proyectos').value);
var anios_dep = Number(datos.anos_depreciacion); //VALOR DE BDD
var interes = parseFloat(document.getElementById('valor_interes').value); //INPUT
var precio = Number(datos.adquisi_depre) //VALOR DE BDD
var elementos = new Array();
var contador = 0
var globalizacion = interes / 100;
console.log(anios_presupuesto, anios_dep, interes,globalizacion, precio, elementos)
var cadena = '';
var valor = 0;
for (var i = 0; i < anios_presupuesto; i += (anios_dep)) {
for (var j = 0; j < anios_dep + 1; j++) {
valor = precio / anios_dep
if (j == 0) {
elementos.push(0);
} else {
elementos.push(valor)
}
if ((contador) <= anios_presupuesto) {
cadena = cadena + `<td class="pre-d td_cant"><input type="text" name="" onkeyup="Generar_Datos_depr(this.value)" value="${(elementos[contador]).toFixed(2)}" readonly></input></td>`
}
contador++
}
precio = precio * (1 + globalizacion)
}
return cadena;
}
Well,
BDD
I'm passing the data as a parameter, so it's not necessary to show it here, if I need it I'll add it later...!
Code get data from BDD
var id_proyecto = document.getElementById('id_proyecto').value;
fetch('/api/flujos_depre/' + id_proyecto)
.then((response) => response.json()).then((response) => {
var cadena = ``
response.forEach(datos => {
cadena += `<tr class="t_fila_inv_a tr_cant">
<td class="pre-i "><input type="text" name="" onkeyup="Generar_Datos_depr(this.value)" value="${datos.concepto_depre}" readonly></td>
${Generar_Datos_depr(datos)}
</tr>`
});
cadena += ``
document.getElementById('flujo_depreciacion').innerHTML = cadena;
});
I was able to solve it in the following way:
In the code where I get the data from the
BDD
I made it a function, and in that function I get the valueinput
I want, I sent values (excuse the redundancy), and I pass it as a parameter so that in the function where it does the calculations I can use them , as well asdatos
.Input:
JS code:
And finally, as the other function said where I do the calculation of the rows, I obtain that value as a parameter and it is only to use it as you want: