I have a table where I add products with quantity and price, and when a product is repeated, I must add the amount of the product that is in the table plus the quantity of the product that is being added. I have a code that does that to me, but only when the product that is in the first row is repeated.
So I urge you to help me so that when a product is repeated no matter what row it is in, if it is the first row or the last row adds me those amounts.
This is my code:
var cont = 0;
var total = 0;
function agregar() {
var cantidad = $("#Cantidad").val();
var IDInsumo = $("#idInsumo").val();
var Precio_Compra = $("#Precio_Compra").val();
var subtotal = Precio_Compra * cantidad;
total = subtotal + total;
console.info(total);
$("#total").val(total);
if (cont == 0) {
cont++;
fila = '<tr class="selected" id="fila' + cont + '" onclick="seleccionar(this.id)"><td>' + cont + '</td><td><input type="hidden" value="' + IDInsumo + '"><input type="hidden" value="' + cantidad + '"><input type="hidden" value="' + Precio_Compra + '">' + IDInsumo + '</td><td>' + cantidad + '</td><td>' + subtotal + '</td></tr>';
$('#tbodydatos').append(fila);
return;
}
var id = "";
var cantidad1 = 0;
var precio1 = 0;
$("#tablaDatos tbody tr").each(function(i, e) {
var tr = $(e);
var td = $(e).find("td").eq(1);
id = $(td).find("input").eq(0).val();
cantidad1 = $(td).find("input").eq(1).val();
precio1 = $(td).find("input").eq(2).val();
var fila = "";
if (id == IDInsumo) {
var tcan = parseInt(cantidad) + parseInt(cantidad1);
var tpre = parseFloat(precio1) + parseFloat(Precio_Compra);
tr.remove();
cont++;
fila = '<tr class="selected" id="fila' + cont + '" onclick="seleccionar(this.id)"><td>' + cont + '</td><td><input type="hidden" value="' + IDInsumo + '"><input type="hidden" value="' + tcan + '"><input type="hidden" value="' + tpre + '">' + IDInsumo + '</td><td>' + tcan + '</td><td>' + tpre + '</td></tr>';
$('#tbodydatos').append(fila);
reordenar();
return false;
} else {
cont++;
fila = '<tr class="selected" id="fila' + cont + '" onclick="seleccionar(this.id)"><td>' + cont + '</td><td><input type="hidden" value="' + IDInsumo + '"><input type="hidden" value="' + cantidad + '"><input type="hidden" value="' + Precio_Compra + '">' + IDInsumo + '</td><td>' + cantidad + '</td><td>' + subtotal + '</td></tr>';
$('#tbodydatos').append(fila);
reordenar();
return false;
}
})
}
function reordenar() {
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" id="idInsumo" placeholder="Insumo" />
<input type="text" id="Cantidad" placeholder="Cantidad" />
<input type="text" id="Precio_Compra" placeholder="Precio de Compra" />
<button onclick="agregar()">Agregar fila</button>
<hr/>
<table id="tablaDatos" class="table table-bordered">
<thead>
<tr>
<td>Numero del Insumo</td>
<td>Insumo</td>
<td>Cantidad</td>
<td>Subtotal</td>
</tr>
</thead>
<tbody id="tbodydatos"></tbody>
<tfoot>
<tr>
<td colspan="3"></td>
<td><input type="text" id="total" disabled /></td>
</tr>
</tfoot>
</table>