0
the goal is to get the subtotals in each row and the totalPedido. It’s happening partially. Two things I’m bothering: 1 - even showing the result in the subtotals, the log keeps displaying the message :create:236 Uncaught Typeerror: Cannot read Property 'value' of Undefined at subtotalize (create:236)"
2 - The total is calculated only when it is the last item of the iteration. And not sum, just concatenate. If I put Parseint( .. does not calculate nor sum.
would like the help to solve these problems, namely: 1 - Calculate subtotals without displaying this error message in the log 2 - calculate the total calculated by being the sum of all of them.
Follow the Lade:
<table class="table">
<tr>
<th scope="col">#</th>
<th scope="col">Produto</th>
<th scope="col">Preço</th>
<th scope="col">Quantidade</th>
</tr>
@foreach ($produtosFazenda as $produtos)
<div class="fazenda">
<tr>
<td>
<input type="hidden" class="produtoid" name = "produtoid[]"
placeholder="{{$produtos->id}}" value="{{$produtos->id}}" aria-label="produto" aria-describedby="basic-addon1" >
</td>
<td>
<p>{{$produtos->descricao}}</p></td>
<td>
<input type="Number" class="valor" name="preco[]" readonly
placeholder="{{$produtos->preco}}" value="{{$produtos->preco}}" aria-label="preco" aria-describedby="basic-addon1" >
</td>
<td>
<input type="Number" class="quantidade" name="quantidade[]"
placeholder="0" aria-label="quantidade" aria-describedby="basic-addon1" onblur = "subtotalizar()" />
</td>
<td>
<input type="Number" class="subtotal" id="subtotal" name = "subtotal[]" readonly
placeholder="0" aria-label="subtotal" aria-describedby="basic-addon1" />
</td>
@endforeach
</tr>
</table>
<input type="Number" class="totalPedido" id="totalPedido" name = "totalPedido" value="00" readonly
placeholder="0" aria-label="totalPedido" aria-describedby="basic-addon1" />
@csrf
<input class ="btn btn-primary" type ="submit" value= "Enviar Pedido">
</form>
</div>
function subtotalizar(){
var itensqtd = document.getElementsByClassName("quantidade");
var itensvlr = document.getElementsByClassName("valor");
var itenssubtotal = document.getElementsByClassName("subtotal");
var totalP = document.getElementById("totalPedido");
for (var i=0; i<=itensvlr.length; i++){
itenssubtotal[i].value = parseInt(itensqtd[i].value) * parseInt(itensvlr[i].value);
totalP.value += parseInt(itenssubtotal[i].value);
}
}
</script>
Thank you in advance.
Thank you very much for the clarifications.
– Iram Faria