Hello fellow StackOverFlow,
I come across the following question:
How can I add all of them input
with name
VN_TOTAL[] when the input with name VN_GRABIVA[] of the same row of the table is empty or equal to zero.
Said total I need to save in the input
with ID siniva .
In the same way, I need to add all of them input
with name
VN_TOTAL[] when the input with name VN_GRABIVA[] of the same row of the table has a value greater than zero.
I need this sum in the input with ID coniva .
In the following image I show the result that I am looking for as an example:
https://i.ibb.co/CsbfqZ1/zrmrf.jpg
$(document).ready(function() {
var tr = 2;
$('#addrowdte').on("click", function() {
$(".details-dte").append('<tr id="'+tr+'"> <td><button onclick="remove_tr('+tr+')" class="btn btn-default btn-sm"><i class="fas fa-trash"></i></button> </td> <td> <input onkeyup="subtotal('+tr+');" type="text" class="input-sm separ-miles cantidad'+tr+'" name="VN_CANTIDA[]" value=""> </td> <td> <div class="input-group"> <input style="min-width: 100px;" type="text" class="form-control input-sm" required="required" id="ADD_CODIGO'+tr+'" name="VN_CODPRO[]" value=""> <a data-toggle="modal" data-target="#modal-search-product" onclick="getrowid('+tr+');" class="input-group-addon input-sm"><i class="fas fa-search"></i></a> </div> </td> <td> <input id="ADD_DETALLE'+tr+'" type="text" class="input-sm" name="VN_DETALLE[]" value=""> </td> <td> <input id="ADD_VALUNI'+tr+'" type="text" class="input-sm separ-miles precio'+tr+'" name="VN_VALUNI[]" value=""> </td> <td> <input type="text" class="input-sm separ-miles subtotal'+tr+'" name="VN_PARCIAL[]" value=""> </td> <td> <input onkeyup="get_total('+tr+');" type="text" class="input-sm separ-miles descuento'+tr+'" name="VN_TASADES[]" placeholder="00,00"> </td> <td> <input id="ADD_VALIVA'+tr+'" type="text" class="input-sm separ-miles iva'+tr+'" name="VN_GRABIVA[]" value=""> </td> <td> <input type="text" class="input-sm separ-miles total'+tr+'" name="VN_TOTAL[]" value=""> </td> </tr>');
tr++;
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.2/css/all.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css" rel="stylesheet"/>
<table class="table table-condensed table-bordered table-hover table-striped">
<thead class="bg-info text-white">
<tr>
<td class="text-center"><a id="addrowdte" class="btn btn-warning btn-xs"><i class="fas fa-plus"></i></a>
</td>
<th>CANTIDAD</th>
<th>CODIGO</th>
<th>NOMBRE</th>
<th>PRECIO</th>
<th>SUB TOTAL</th>
<th>DCTO %</th>
<th>IVA %</th>
<th>TOTAL</th>
</tr>
</thead>
<tbody class="details-dte">
<tr id="1">
<td>
<button onclick="remove_tr(1)" class="btn btn-default btn-sm"><i class="fas fa-trash"></i>
</button>
</td>
<td>
<input onkeyup="subtotal(1);" type="text" class="input-sm separ-miles cantidad1" name="VN_CANTIDA[]" value="">
</td>
<td>
<div class="input-group">
<input style="min-width: 100px;" type="text" class="form-control input-sm" required="required" id="ADD_CODIGO1" name="VN_CODPRO[]" value="">
<a data-toggle="modal" data-target="#modal-search-product" onclick="getrowid(1);" class="input-group-addon input-sm"><i class="fas fa-search"></i></a>
</div>
</td>
<td>
<input id="ADD_DETALLE1" type="text" class="input-sm" name="VN_DETALLE[]" value="">
</td>
<td>
<input id="ADD_VALUNI1" type="text" class="input-sm separ-miles precio1" name="VN_VALUNI[]" value="">
</td>
<td>
<input type="text" class="input-sm separ-miles subtotal1" name="VN_PARCIAL[]" value="">
</td>
<td>
<input onkeyup="get_total(1);" type="text" class="input-sm separ-miles descuento1" name="VN_TASADES[]" placeholder="00,00">
</td>
<td>
<input id="ADD_VALIVA1" type="text" class="input-sm separ-miles iva1" name="VN_GRABIVA[]" value="">
</td>
<td>
<input type="text" class="input-sm separ-miles total1" name="VN_TOTAL[]" value="">
</td>
</tr>
</tbody>
</table>
<script>
function remove_tr(id) {
event.preventDefault();
if (id != 1) {
$('#' + id).remove();
}
}
function subtotal(idrow) {
var rowid = idrow;
var cantidad, precio, total, iva;
if ($('.cantidad' + rowid).val().length > 0) {
cantidad = parseFloat($('.cantidad' + rowid).val());
} else {
cantidad = 0;
}
if ($('.precio' + rowid).val().length > 0) {
precio = parseFloat($('.precio' + rowid).val());
} else {
precio = 0;
}
total = (precio * cantidad);
$('.subtotal' + rowid).val(total);
if ($('.iva' + rowid).val().length > 0) {
iva = parseFloat($('.iva' + rowid).val());
iva = (total * iva / 100);
total = (total + iva);
}
$('.total' + rowid).val(total);
};
function get_total(idrow) {
var rowid = idrow;
var subtotal, descuento, total, iva;
subtotal = parseFloat($('.subtotal' + rowid).val());
if ($('.descuento' + rowid).val().length <= 0) {
descuento = 0;
} else {
descuento = parseFloat($('.descuento' + rowid).val());
}
total = (subtotal * descuento / 100);
total = (subtotal - total);
if ($('.iva' + rowid).val().length > 0) {
iva = parseFloat($('.iva' + rowid).val());
iva = (total * iva / 100);
total = (total + iva);
}
$('.total' + rowid).val(total);
}
</script><br>
<span>Total sin iva:</span>
<input type="text" id="siniva" value="">
<br><br>
<span>Total con iva:</span>
<input type="text" id="coniva" value="">
Any questions or news I will be very aware.