I have a table where I enter some products with <select>
which they have a value that I am showing with fetch , what I need is that according to their final cost value, determine a percentage,
Well, in the table I have quantity, unit price, acquisition value (quantity * pre) and the percentage.
Now the percentage if you are calculating it but not in the correct way, it should be like this:
percentage = ((acquisition_value / total_sum)*100)
percentage = ((448/ 1344)*100) = 33.33%
I need help, with the calculation of the percentage, the javascript code:
function Total_activos() {
const rows = document.getElementsByClassName("t_calculo_activo");
let total = 0;
let porcent = 0
for (var i = 0; i < rows.length; i++) {
const cantidad = rows[i].getElementsByClassName('valor_cant')[0].value;
const precio = rows[i].getElementsByClassName('valor_prec')[0].value;
const valor_Adqui = (cantidad * precio);
rows[i].getElementsByClassName('valor_adqui')[0].value = valor_Adqui.toFixed(2);
total += valor_Adqui;
porcent = total
const valor_porcen = ((valor_Adqui / porcent)*100);
rows[i].getElementsByClassName('valor_porcentaje')[0].value = valor_porcen.toFixed(2);
}
var activo_suma = total;
document.getElementById('total_adqui').innerHTML = activo_suma.toFixed(2);
}
HTML
<tbody id="t_addfilas_acti" class="t-inversiones-body">
@php ($cant_tivo = 1)
@foreach ($inversiones_activos as $item)
<tr class="t_fila_inv_a t_calculo_activo" id="tr_iver_act_{{$cant_tivo}}">
<td>
<select name="tipo_activo[]" id="select_opcion_ac_{{$cant_tivo}}" onkeyup="Total_activos(this);" onchange="OpcionChange_ac('select_opcion_ac_{{$cant_tivo}}', 'select_escoger_ac_{{$cant_tivo}}')" class=" form-control form-control-sm-custom">
@if ( $item->tipo_activo == null )
<option value="0">Seleccionar...</option>
@else
<option value="{{$item->tipo_activo}}">{{$item->tipo_activo}}</option>
@endif
@foreach ($proformas as $item2)
<option value="{{$item2->descripcion}}" >{{$item2->descripcion}}</option>
@endforeach
</select>
</td>
<td colspan="2"><input type="text" name="activos_fijos[]" readonly value="{{$item->activos_fijos}}"></td>
{{-- cantidad --}}
<td class="pre-c"><input type="number" step="0.01" name="cantidad_fijos[]" class="valor_cant" onkeyup="Total_activos();" value="{{$item->cantidad_fijos}}"></td>
{{-- precio unidad --}}
<td class="pre-d" id="select_escoger_ac_{{$cant_tivo}}">
<input type="text" step="0.01" name="precio_fijos[]" class="valor_prec" onkeyup="Total_activos();" readonly value="{{$item->precio_fijos}}">
</td>
{{-- valor Adquisición --}}
<td class="pre-d"><input type="number" step="0.01" name="valor_fijos[]" class="valor_adqui" onkeyup="Total_activos();" readonly value="{{$item->valor_fijos}}"></td>
{{-- porcentaje --}}
<td class="pre-d"><input type="number" step="0.01" name="porcentaje_fijos[]" class="valor_porcentaje" onkeyup="Total_activos();" readonly value="{{$item->porcentaje_fijos}}"></td>
<td><i class="fas fa-trash"></i></td>
</tr>
@php ($cant_tivo++)
@endforeach
</tbody>
As you can see in the image, if it does it correctly but it does not modify the previous ones and that is what I need, to calculate the percentage and automatically change the rest.
In this case you need two loops, one to calculate the total and the other to assign percentages. You can do it like this: