3
Good Afternoon, I need a great help, I’m caught in a problem that apparently should be very easy! but I’m not managing to evolve.
I have a code with the following fields:
In my script consists of 5 groups of apparent inputs and a button to add more inputs (dynamic inputs), my problem is in the sum of inputs.
1 - I need to multiply the inputs "Quantity" X "Value Und" and its result appear in the input "Total". In my current code this multiplication is already being done, what I need is that the inputs "Quantity" and "Value Und" accept the "," Ex. ( 5,50 ).Because I am working with units of measure and money. (This function should be valid for all fields even the dynamics)
2 - I need to sum all with inputs "Vltotal" ( even the dynamics ) + the input "other" and its result appears in the field "totalnfe". Which should be in Money format R$. Ex( 2.50 or 2.005,50)
As I said, I believe it’s such a simple thing, only I’m not getting it..
Thanks in advance!
<script>
$(function () {
//Initialize Select2 Elements
$(".select2").select2();
//Date picker
$('#datae').datepicker({
format: 'dd/mm/yyyy',
todayBtn: 'linked',
language: 'pt-BR',
autoclose: true,
todayHighlight: true
});
$('#datav').datepicker({
format: 'dd/mm/yyyy',
todayBtn: 'linked',
language: 'pt-BR',
autoclose: true,
todayHighlight: true
});
});
</script>
<script type="text/javascript">
$(document).ready(function () {
var i = 5;
//adiciona nova linha
$("#add").on("click", function () {
i++;
var newRow = $("<tr>");
var cols = "";
cols += '<td><input type="text" class="form-control" id="cod" name="cod[]"></td>';
cols += '<td><input type="text" class="form-control" id="desc" name="desc[]"></td>';
cols += '<td><input type="text" class="form-control" id="und" name="und[]"></td>';
cols += '<td><input type="text" class="form-control" id="qnd' + i + '" name="qnd[]"></td>';
cols += '<td><input type="text" class="form-control" id="vlund' + i + '" name="vlund[]"></td>';
cols += '<td><input type="text" class="form-control soma" id="vltotal' + i + '" name="vltotal[]" onblur="calcular()"></td>';
cols += '<td><button type="button" name="remove" id="'+i+'" class="btn btn-danger deleteLinha">X</button></td>';
newRow.append(cols);
$("#products-table").append(newRow);
});
//chamada da função para calcular o total de cada linha
$("#products-table").on("blur keyup", 'input[id^="vlund"], input[id^="qnd"]', function (event) {
calculateRow($(this).closest("tr"));
});
//remove linha
$("#products-table").on("click", "button.deleteLinha", function (event) {
$(this).closest("tr").remove();
});
});
//função para calcular o total de cada linha
function calculateRow(row) {
var vlund = +row.find('input[id^="vlund"]').val();
var qnd = +row.find('input[id^="qnd"]').val();
//2 casas decimais
var total = (vlund * qnd).toFixed(2);
//substitui ponto por virgula
total=total.replace(".", ",");
//a regex abaixo coloca um ponto a esquerda de cada grupo de 3 digitos desde que não seja no inicio do numero
row.find('input[id^="vltotal"]').val((total).replace(/\B(?=(\d{3})+(?!\d))/g, "."));
}
$(".soma").blur(function(){
calcular();
});
function calcular() {
var soma = 0;
$(".soma").each(function(indice, item){
var valor = parseFloat($(item).val());
console.log(valor);
if ( !isNaN( valor ) ) {
soma += valor;
}
});
$("#totalnfe").val(soma)
}
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<table class="table" id="products-table">
<thead>
<tr>
<th>Cod.</th>
<th>Descrição</th>
<th>Und.</th>
<th>Quantidade</th>
<th>R$: UND</th>
<th>R$: TOTAL</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" class="form-control cod" id="cod" name="cod[]"></td>
<td><input type="text" class="form-control desc" id="desc" name="desc[]"></td>
<td><input type="text" class="form-control und" id="und" name="und[]"></td>
<td><input type="text" class="form-control" id="qnd" name="qnd[]"></td>
<td><input type="text" class="form-control" id="vlund" name="vlund[]" onblur="calcular()"></td>
<td><input type="text" class="form-control soma" id="vltotal" name="vltotal[]" onblur="calcular()"></td>
</tr>
<tr>
<td><input type="text" class="form-control cod" id="cod" name="cod[]"></td>
<td><input type="text" class="form-control desc" id="desc" name="desc[]"></td>
<td><input type="text" class="form-control und" id="und" name="und[]"></td>
<td><input type="text" class="form-control" id="qnd" name="qnd[]"></td>
<td><input type="text" class="form-control" id="vlund" name="vlund[]"></td>
<td><input type="text" class="form-control soma" id="vltotal" name="vltotal[]" onblur="calcular()"></td>
</tr>
<tr>
<td><input type="text" class="form-control" id="cod" name="cod[]"></td>
<td><input type="text" class="form-control" id="desc" name="desc[]"></td>
<td><input type="text" class="form-control" id="und" name="und[]"></td>
<td><input type="text" class="form-control" id="qnd" name="qnd[]"></td>
<td><input type="text" class="form-control" id="vlund" name="vlund[]"></td>
<td><input type="text" class="form-control soma" id="vltotal" name="vltotal[]" onblur="calcular()"></td>
</tr>
<tr>
<td><input type="text" class="form-control" id="cod" name="cod[]"></td>
<td><input type="text" class="form-control" id="desc" name="desc[]"></td>
<td><input type="text" class="form-control" id="und" name="und[]"></td>
<td><input type="text" class="form-control" id="qnd" name="qnd[]"></td>
<td><input type="text" class="form-control" id="vlund" name="vlund[]"></td>
<td><input type="text" class="form-control soma" id="vltotal" name="vltotal[]" onblur="calcular()"></td>
</tr>
<tr>
<td><input type="text" class="form-control" id="cod" name="cod[]"></td>
<td><input type="text" class="form-control" id="desc" name="desc[]"></td>
<td><input type="text" class="form-control" id="und" name="und[]"></td>
<td><input type="text" class="form-control" id="qnd" name="qnd[]"></td>
<td><input type="text" class="form-control" id="vlund" name="vlund[]"></td>
<td><input type="text" class="form-control soma" id="vltotal" name="vltotal[]" onblur="calcular()"></td>
</tr>
<tr class="row">
</tr>
</tbody>
<tfoot>
<tr>
<td><button type="button" name="add" id="add" class="btn btn-success">Adicionar Linhas +</button></td>
<td></td>
<td></td>
<td></td>
<td><label for="totalnfe">Outras Despesas</label><input type="text" class="form-control" id="outras" name="outras"></td>
<td><label for="totalnfe">Valor Total da Nota</label><input type="text" class="form-control" id="totalnfe" name="totalnfe" readonly></td>
</tr>
</tfoot>
</table>
</html>
these Date Picker functions are giving error, missing library.
– user60252