I am not able to generate an overall total of my table, in each row I have the quantity and price. I need to generate the total value.
The current logic is reading all the rows of the table and accumulating the total of the rows. The problem is in the first line that returns undefined that generates a NaN (Not a number), the others are being loaded correctly.
<table class="table" id="table">
<thead>
<tr><th>Código</th>
<th>Descrição</th>
<th style="width: 10%;">Quantidade</th>
<th>Preço</th>
<th>Valor total</th>
<th>Ação</th>
</tr></thead><tbody>
<tr>
<td>1</td>
<td>Processador I7</td>
<td><input class="form-control quantidade" id="id_form-0-quantidade" min="0" name="form-0-quantidade" oninput="atualizarDinamico1(this)" type="number" value="6"></td>
<td><input class="form-control preco" id="id_form-0-preco" name="form-0-preco" oninput="atualizarDinamico1(this)" step="0.01" type="number" value="150.00"></td>
<td>900.00</td>
<td><span id="form-0-DELETE" class="total"></span></td>
<td class="text-center">
<button value="on" type="submit" name="form-0-DELETE" class="btn btn-danger btn-sm">
<span class="glyphicon glyphicon-trash"></span> Excluir</button>
<input id="id_form-0-id" name="form-0-id" type="hidden" value="1">
</td>
</tr>
<tr>
<td>5</td>
<td>Teclado Dell</td>
<td><input class="form-control quantidade" id="id_form-1-quantidade" min="0" name="form-1-quantidade" oninput="atualizarDinamico1(this)" type="number" value="1"></td>
<td><input class="form-control preco" id="id_form-1-preco" name="form-1-preco" oninput="atualizarDinamico1(this)" step="0.01" type="number" value="105.00"></td>
<td>105.00</td>
<td><span id="form-1-DELETE" class="total"></span></td>
<td class="text-center">
<button value="on" type="submit" name="form-1-DELETE" class="btn btn-danger btn-sm">
<span class="glyphicon glyphicon-trash"></span> Excluir</button>
<input id="id_form-1-id" name="form-1-id" type="hidden" value="22">
</td>
</tr>
<tr>
<td>4</td>
<td>Mouse XPTO</td>
<td><input class="form-control quantidade" id="id_form-2-quantidade" min="0" name="form-2-quantidade" oninput="atualizarDinamico1(this)" type="number" value="1"></td>
<td><input class="form-control preco" id="id_form-2-preco" name="form-2-preco" oninput="atualizarDinamico1(this)" step="0.01" type="number" value="120.00"></td>
<td>120.00</td>
<td><span id="form-2-DELETE" class="total"></span></td>
<td class="text-center">
<button value="on" type="submit" name="form-2-DELETE" class="btn btn-danger btn-sm">
<span class="glyphicon glyphicon-trash"></span> Excluir</button>
<input id="id_form-2-id" name="form-2-id" type="hidden" value="23">
</td>
</tr>
</tbody>
</table>
Script:
$("#myBtn1").click(function(){
var total = 0
$("#table tr").each(function(){
var currentRow=$(this);
var qtd=currentRow.find(".quantidade").val();
var preco=currentRow.find(".preco").val();
total = total + (parseFloat(qtd) * parseFloat(preco));
console.log(qtd);
console.log(preco);
});
console.log(total);
});
Console:
(index):319 undefined
(index):320 undefined
(index):319 6
(index):320 150.00
(index):319 1
(index):320 105.00
(index):319 1
(index):320 120.00
(index):323 NaN