If item exists in the list, add 1 more

Asked

Viewed 41 times

-1

 var prodAdd = false;
				 $(".id").each(function(index, value) {
	         if ($(value).val() == vId) {
						 Swal({
		         	type: 'info',
		         	html: '<h5>Produto ja adicionado!</h5>',
		         	background: 'radial-gradient(#f1f1f1,LightGray)',
		         	showConfirmButton: true,
		         })
             $('#pqnt').val(vQnt + 1)
	           prodAdd = true;            
	         }
	       });


         	if (!prodAdd) {

            var cont=0;

            var linha = '<tr class="selected" id="linha'+cont+'">'+
            '<td>'+
            '<input class="id" type="hidden" name="idproduto[]" value="'+vId+'">'+vProduto+
            '</td>'+
            '<td>'+
            '<input id="preco" type="text" class="form-control text-right" name="preco[]" value="'+vPreco+'" onblur="multiplicar();" onblur="formatar();">'+
            '</td>'+				
            '<td>'+
            '<input id="pqnt" type="text" class="form-control text-center" name="qnt[]" value="'+vQnt+'"><span class="vlr">'+			
            '</td>'+	
            '<td>'+
            '<input  style="text-align: right;" id="subtotal" class="form-control subtotal" type="hidden" value="'+vTotal+'"  readonly  onblur="calcular();">'+
            '<input  style="text-align: right;" type="text" class="form-control subtotalT" id="subtotalT" name="subtotal[]" value="'+numberToReal(vTotal)+'" readonly>'+
            '</td>'+	
            '<td class="text-center">'+
            '<a style="cursor:pointer" onclick="deleteRow(this)" class="glyphicon glyphicon-trash"></a>'+
            '</td>'+
            ' </tr>'
            cont++;
            $('#mytbody').append(linha);
            contItem();
            calcular();          


            $(".subtotal").each(function() {
                $(this).blur(function(){                 
                    calcular();
                });
            });


          }
   
  } 
 <table id="tab" class="table table-striped" style="width: 100%;table-layout:fixed">
                      <thead>
                        <tr>
                          <th id="tab_cab" style="width:15%">Produto</th>
                          <th id="tab_cab" style="width: 8%">Preço</th>
                          <th id="tab_cab" style="width: 7%">Qnt</th>  
                          <th id="tab_cab" style="width: 9%">SubTotal</th>
                          <th id="tab_cab" style="width: 3%;border-left:solid 1px">...</th>
                        </tr>
                      </thead>
                      <tbody id="mytbody">
                      
                      </tbody>            
                    </table>

Good afternoon, everyone.

I’m making a pdv screen and would like that when I add the same item more than once, it just increment in quantity and vice versa, when I remove it, it removes in quantity one by one.

inserir a descrição da imagem aqui

  • edit the question and enter the code to make it easier to help you.

  • thanks for the return Otaviocapel . See my code above. However it increments once and for.

1 answer

0

I imagine you control the products using some product identifier, something like "product-134", use a .length to count the amount of the same product, I hope to have helped.

  • thank you for the return Dev_ps. Yes, you have idenficator. See my code above. However, it increments once and for

  • Have you tried to return the value of prodAdd for false at the end of the code?

  • I tried, but it didn’t work. Unfortunately.

Browser other questions tagged

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