take jquery autocomplete value for dynamic online calculation

Asked

Viewed 88 times

0

I need to take the returned value of the autocomplete to make a sum in a dynamic list of products, and already does the calculation of value and quantity and now I needed to insert a new column "MEDIDA" it is necessary to also remove the letters and take only the Qtd of the measure, I followed the code:

autocomplete

// MEDIDA

var x_medida = [

   "1 UNIDADE",
    "PCT 10",
    "PCT 20",
    "PCT 30",
    "PCT 40",
    "PCT 50"

];

$( ".medida" ).autocomplete({ source: x_medida });

Dynamic table

contador++;


var newRow = $("<tr>");
var cols = "";

cols += '<td class="contador valor_total" >' + contador + '</td>';
cols += '<td><label text-align="center"><input type="text" name="produto' + contador + '" class="produto" /></label></td>';
cols += '<td><label text-align="center"><input type="text" name="medida'  + contador + '" class="medida centro" /></label></td>';
cols += '<td><label text-align="center"><input type="text" name="qtd'     + contador + '" class="qtd centro" onkeyup="somenteNumeros(this);" /></label></td>';
cols += '<td><label text-align="center"><input type="text" name="preco'   + contador + '" class="preco centro"  align="center" /></label></td>';
cols += '<td class="col-md-2 centro"><b> R$ 0.00 </b></td>';
cols += '<td><a class="deleteLinha"> Excluir </a></td>';

newRow.append(cols);

$("#products-table").append(newRow);

sum of line values

// FUNCTION ( CALCULA TOTAL DA LINHA ) 

function calculateRow(row) {

    var preco    = +row.find('input[name^="preco"]').val();
    var qtd      = +row.find('input[name^="qtd"]').val();
    //var medida   = +row.find('input[name^="medida"]').val();

    //2 casas decimais
    var total = (preco * qtd).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('.total').html("R$ " + (total).replace(/\B(?=(\d{3})+(?!\d))/g, "."));     

}

1 answer

1


Can use match with regex below to get only the value numbers:

var medida   = +row.find('input[name^="medida"]').val().match(/\d+/);

If the value is: "PCT 10", will return "10". And you can use this value to include in your calculations.

  • Perfect friend, I tried so much and it was simpler than I imagined .... kkkkk obg ae !!!

Browser other questions tagged

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