Function to totalize selected input values in checkbox

Asked

Viewed 411 times

1

Good afternoon.

I have a table on my page with columns of quantity and value of the product. These values are returned from my BD, in each row there is a checkbox for selection, I have already made a function to calculate the total of each product (quantity * value). What is needed is a function to sum up the products selected by the checkbox.

HTML/PHP

<table id="produtos" class="table table-striped table-condensed table-bordered  table-hover">

  <thead>
    <tr>
      <th style="width:10%">Produto</th>
      <th style="width:30%">Descricao</th>
      <th>Quantidade</th>
      <th>Valor Tabela</th>
      <th>Ultima Compra</th>
      <th style="width:5%">Emb.</th>
      <th style="width:5%">Peso Bruto</th>
      <th style="width:5%">Est. Casa</th>
      <th style="width:10%">Est. Cliente</th>
      <th style="width:10%">Qtd. Pedido</th>
      <th style="width:10%">Valor compra</th>
      <th style="width:20%">Total</th>
      <th>Sel.</th>
    </tr>
  </thead>
  <tbody>
    <?php $i = 0; 
      do {  ?>
    <tr>
      <td><?php echo $row_vendas['produto']; ?><input type="hidden" name="produto[]" value="<?php echo $row_vendas['id_produto'] ?>"></td>
      <td><?php echo $row_vendas['descricao']; ?><input type="hidden" name="descricao[]" value="<?php echo $row_vendas['descricao'] ?>"></td>
      <td><?php echo number_format($row_vendas['quantidade'],3,',','.'); ?></td>
      <td><?php echo number_format($row_vendas['preco1'],2,',','.'); ?></td>
      <td><?php echo date('d/m/Y', strtotime($row_vendas['ultima_compra'])); ?></td>
      <td><?php echo $row_vendas['embalagem']; ?></td>
      <td><?php echo $row_vendas['peso_bruto']; ?><input type="hidden" name="peso[]" value="<?php echo $row_vendas['peso_bruto'] ?>"></td>
      <td><?php echo number_format($row_vendas['estoque'],3,',','.'); ?></td>
      <td><input type="number" min="1" name="estoque" style="width:80%" autocomplete="off"></td>
      <td><input type="number" min="1" name="qtd[]" id="qtd<?php echo $i ?>" style="width:80%" autocomplete="off" onChange="somaProduto(<?php echo $i; ?>)"></td>
    <td>
      <input type="text" name="valor[]" id="preco<?php echo $i; ?>" style="width:70%" value="<?php echo number_format($row_vendas['valor'],2,',','.') ?>" autocomplete="off" onkeypress="return SomenteNumero(this, event);" onChange="somaProduto(<?php echo $i; ?>)">
      <input type="hidden" id="tolerancia<?php echo $i; ?>" style="width:80%" value="<?php echo $row_vendas['tolerancia_preco'] ?>" autocomplete="off">
      <input type="hidden" id="valTabela<?php echo $i; ?>" style="width:80%" value="<?php echo $row_vendas['preco1'] ?>" autocomplete="off">
    </td>
    <td><input type="text" name="totalProduto" disabled class="disabled" id="totalProduto<?php echo $i?>" style="width:80%" autocomplete="off"></td>
    <td><input type="checkbox" onClick="SomaTotais(<?php echo $i; ?>)" name="selecao[]" id="selecao<?php echo $i?>" value="<?php echo $i; ?>" style="width: 30px; height: 30px;"></td>              
  </tr>
  <?php $i = $i+1; } while ($row_vendas = mysql_fetch_assoc($vendas));?>
</tbody>
</table>

Javascript

function somaProduto(nro){
    var qtd = $('#qtd'+nro+'').val();
    var valUnit = DesFormataMoeda($('#preco'+nro).val());

    total   = qtd * valUnit;

    $('#totalProduto'+nro+'').val(ConverteMoeda(total));
}

I did this function, but it’s still not adding up the values:

function SomaTotais(nro){
    var total = 0;
    $("input:checkbox[data-id=selecao]:checked").each(function () {
            var i = this.value;
            var valor = DesFormataMoeda($('#totalProduto'+i).val());                    
            total += valor              
        });
        alert(ConverteMoeda(total));

}
  • 2

    Post your html code there, so we know how your structure is and we can help you with the code.

  • 3

    Welcome to Stackoverflow PT, so it is easier to help you include in your question the HTML code of the table, and the functions that are already working

  • 1

    Just complementing what @Sanction said, I recommend reading, How to ask a good question?

  • I reversed your title edition because here we do not use this space to mark that the question has been solved. See How and why to accept an answer? (you will need to wait a system deadline before you can mark your own response as accepted).

  • ok thanks @bfavaretto

1 answer

2


Solved here, I changed function and worked as wanted.

function SomaTotais(){
	var total = 0;
	$("input:checkbox[data-id=selecao]:checked").each(function () {
		var i = this.value;
        var valor = Number(DesFormataMoeda($('#totalProduto'+i).val()));						
		total += valor;
		$('#total').html(total);				
	});			
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.