I have a solution that does the sum of values with respect to the value of a input check
only if it is selected, but if you select the items from first to last, the sum is normally done but only the last input
of the table the value is zeroed, sample code follows.
function selecionaDactes() {
var ids = document.getElementsByClassName('editar');
gravaArray(ids);
}
function gravaArray(dados) {
var array_dados = dados;
var teste = Array();
var valor = Array();
for (var x = 0; x <= array_dados.length; x++) {
if (typeof array_dados[x] == 'object') {
if (array_dados[x].checked) {
teste[x] = array_dados[x].id;
valor[x] = parseFloat(array_dados[x].value);
}
}
}
teste = teste.filter(Boolean);
document.getElementById('docs').value = teste;
valorTotal(valor);
}
function valorTotal(valor) {
var array_valores = valor;
var total = 0;
for (var a = 0; a <= Object.keys(array_valores).length; a++) {
if (array_valores[a] != '' && array_valores[a] != undefined && array_valores[a] != NaN) {
total = parseFloat(total + array_valores[a]);
}
}
total = total.toLocaleString('pt-br', {
minimumFractionDigits: 2,
currency: 'BRL'
});
document.getElementById('valor').value = total;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><!--LatestcompiledandminifiedCSS--><linkrel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<form name="teste" method="#" action="">
<label>Documents:
<input type="text" name="docs" id="docs"/></label>
<br>
<label>Values:
<input type="text" name="valor" id="valor"/></label>
<br>
</form>
<table id="tbl_dactes" border="0" class="table table-condensed table-hover">
<thead>
<tr>
<th align="center">#</th>
<th>Transportador</th>
<th>Conhecimento</th>
<th>R$ Frete</th>
<th>Data Emissão</th>
<th>Fornecedor</th>
<th><span class="glyphicon glyphicon-pencil"></span></th>
</tr>
</thead>
<tbody>
<tr>
<td><input class="editar" type="checkbox" name="checkboxes" id="438283" value="65.58"> </td>
<td>Dll logistica</td>
<td>438283</td>
<td><span class="valor_dacte">R$ 65,58</span></td>
<td>03/05/2017</td>
<td>Aliexpress</td>
<td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
<!-- inicio modal -->
</tr>
<tr>
<td><input class="editar" type="checkbox" name="checkboxes" id="3171" value="73.62"> </td>
<td>Inter Modal</td>
<td>3171</td>
<td><span class="valor_dacte">R$ 73,62</span></td>
<td>05/05/2017</td>
<td>Ebay</td>
<td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
<!-- inicio modal -->
</tr>
<tr>
<td><input class="editar" type="checkbox" name="checkboxes" id="191513" value="65"> </td>
<td>Italian Group Global</td>
<td>191513</td>
<td><span class="valor_dacte">R$ 65,00</span></td>
<td>09/05/2017</td>
<td>FastBay</td>
<td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
<!-- inicio modal -->
</tr>
<tr>
<td><input class="editar" type="checkbox" name="checkboxes" id="18977" value="157.74"> </td>
<td>All In Global</td>
<td>18977</td>
<td><span class="valor_dacte">R$ 157,74</span></td>
<td>08/05/2017</td>
<td>Tesla</td>
<td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
<!-- inicio modal -->
</tr>
<tr>
<td><input class="editar" type="checkbox" name="checkboxes" id="18969" value="181.32"> </td>
<td>Inter Modal</td>
<td>18969</td>
<td><span class="valor_dacte">R$ 181,32</span></td>
<td>08/05/2017</td>
<td>Adidas</td>
<td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
<!-- inicio modal -->
</tr>
<tr>
<td><input class="editar" type="checkbox" name="checkboxes" id="190999" value="65"> </td>
<td>Global Latin</td>
<td>190999</td>
<td><span class="valor_dacte">R$ 65,00</span></td>
<td>04/05/2017</td>
<td>Footage</td>
<td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
<!-- inicio modal -->
</tr>
<tr>
<td><input class="editar" type="checkbox" name="checkboxes" id="1296" value="253.34"> </td>
<td>E-Log Transportes Rodoviarios LTDA</td>
<td>1296</td>
<td><span class="valor_dacte">R$ 253,34</span></td>
<td>09/05/2017</td>
<td>Nike</td>
<td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
<!-- inicio modal -->
</tr>
<tr>
<td><input class="editar" type="checkbox" name="checkboxes" id="431587" value="191.25"> </td>
<td>Vast</td>
<td>431587</td>
<td><span class="valor_dacte">R$ 191,25</span></td>
<td>30/03/2017</td>
<td>Etti</td>
<td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
<!-- inicio modal -->
</tr>
<tr>
<td><input class="editar" type="checkbox" name="checkboxes" id="5161" value="400.92"> </td>
<td>Tico Tico</td>
<td>5161</td>
<td><span class="valor_dacte">R$ 400,92</span></td>
<td>29/04/2017</td>
<td>LaCoste</td>
<td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
<!-- inicio modal -->
</tr>
<tr>
<td><input class="editar" type="checkbox" name="checkboxes" id="18216" value="148.8"> </td>
<td>Italian Log</td>
<td>18216</td>
<td><span class="valor_dacte">R$ 148,80</span></td>
<td>21/04/2017</td>
<td>Vine</td>
<td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
<!-- inicio modal -->
</tr>
<tr>
<td><input class="editar" type="checkbox" name="checkboxes" id="435335" value="377.15"> </td>
<td>Porto Ferreira Portugal xpress</td>
<td>435335</td>
<td><span class="valor_dacte">R$ 377,15</span></td>
<td>18/04/2017</td>
<td>Sardinha</td>
<td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
<!-- inicio modal -->
</tr>
</tbody>
</table>
If I select only the last 2 records the value becomes 0.00, I am not finding the error, I believe the error is when generating the array with the values.