Calculate subtotal and total with javascript

Asked

Viewed 333 times

-1

Hello, I would like to know how do for the quantity of items in the table row be multiplied by the unit value of the same row forming a sub-total to be totaled at the end. In my code, until then I could only calculate the total value only adding without passing the units to be multiplied by the unit value.

var atualizaCarrinho = function(){
    var carrinhos = $('.carrinho');
    carrinhos.each(function(){
        var carrinhoAtual = $(this);
        var valorItem = carrinhoAtual.find('.item-total:visible');
        var valorTotal = carrinhoAtual.find('.valorTotal');
        var qtdTotal = carrinhoAtual.find('.qtdProdutos');
        var resultado = 0;

        valorItem.each(function(){
            var tdAtual = $(this);
            var pegaValor = parseFloat(tdAtual.text());
            resultado = parseFloat(resultado + pegaValor);
        });

        valorTotal.text(resultado);
        qtdTotal.text(valorItem.length);
    });
};
atualizaCarrinho();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="carrinho">
		<h2>Carrinho entrega em Santa Catarina</h2>
		<table border=1>
			<thead>
				<tr>
					<td>Nome</td>
					<td>Quantidade</td>
					<td>Valor Unitário</td>
					<td>SubTotal</td>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td>Tablet miPad 18</td>
					<td class="qtd_item">6</td>
					<td>499.0</td>
					<td class="item-total">499.0</td>
				</tr>
				<tr>
					<td>Tablet miPad 19</td>
					<td class="qtd_item">1</td>
					<td>599.0</td>
					<td class="item-total">599.0</td>
				</tr>
			</tbody>
		</table>
		<div><strong>Valor total: </strong> R$ <span class="valorTotal">0</span></div>
		<div><strong>Quantidade de Produtos: </strong> <span class="qtdProdutos">0</span></div>
	</div>

1 answer

0


Thus?

var atualizaCarrinho = function(){
    var carrinhos = $('.carrinho');
    carrinhos.each(function(){
        var carrinhoAtual = $(this);
        var valorItem = carrinhoAtual.find('.item-total:visible');
        var valorTotal = carrinhoAtual.find('.valorTotal');
        var qtdTotal = carrinhoAtual.find('.qtdProdutos');
        var item_total = $('.item-total');
        var qtdItems = $('.qtd_item');
        var vt= 0;
        var resultado = 0.0;
        for(var i = 0; i < qtdItems.length; i++){
            var qtdItem = parseFloat(qtdItems[i].innerHTML);
            var vtItem = qtdItem * parseFloat(item_total[i].innerHTML);
            item_total[i].innerHTML = vtItem;
            resultado += vtItem;
            vt += qtdItem ;
        }   
                
        valorTotal.text(resultado);
        qtdTotal.text(vt);
    });
};
atualizaCarrinho();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="carrinho">
		<h2>Carrinho entrega em Santa Catarina</h2>
		<table border=1>
			<thead>
				<tr>
					<td>Nome</td>
					<td>Quantidade</td>
					<td>Valor Unitário</td>
					<td>SubTotal</td>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td>Tablet miPad 18</td>
					<td class="qtd_item">6</td>
					<td>499.0</td>
					<td class="item-total">499.0</td>
				</tr>
				<tr>
					<td>Tablet miPad 19</td>
					<td class="qtd_item">1</td>
					<td>599.0</td>
					<td class="item-total">599.0</td>
				</tr>
			</tbody>
		</table>
		<div><strong>Valor total: </strong> R$ <span class="valorTotal">0</span></div>
		<div><strong>Quantidade de Produtos: </strong> <span class="qtdProdutos">0</span></div>
	</div>

  • Thanks, I had tried to use the for, but it was a mistake. But now I understood the logic.

  • Use classes to identify the tds and the elements is not very good, you should use id’s. Thank goodness it helped, put the answer as accepted.

Browser other questions tagged

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