-1
I have the code below, and I am using the autocomplete with Blur so that the user enters the barcode, he consults in the bank and fills the fields automatically. Since I don’t have javascript domain I couldn’t get out of it.
//+ produtos
$(document).ready(function () {
var max_fields = 32;
var wrapper = $(".produto"); //Fields wrapper
var add_button = $(".add-camera"); //Add button ID
var x = 0; //initlal text box count
$(add_button).click(function (e) { //on add input button click
e.preventDefault();
if (x < max_fields) { //max input box allowed
x++; //text box increment
$(wrapper).append('<div class="form-group">\
<input type="text" name="idproduto' + x + '" class="form-control produtoCounter idproduto" id="idproduto' + x + '" Size="1" placeholder="Id" readonly />\
<input type="text" name="ean' + x + '" class="form-control produtoCounter" id="ean' + x + '" Size="10" placeholder="ean" />\
<input type="text" name="nome' + x + '" class="form-control produtoCounter" id="nome' + x + '" Size="50" placeholder="Nome" required autofocus />\
<input type="text" name="referencia' + x + '" class="form-control produtoCounter" id="referencia' + x + '" Size="10" placeholder="Referencia" readonly />\
<input type="text" name="quantidade' + x + '" id="quantidade' + x + '" class="form-control produtoCounter quantidade" Size="2" placeholder="Quant." onchange="somarTotais()" />\
<input type="text" name="valorunitario' + x + '" id="valorunitario' + x + '" onkeydown="FormataMoeda(this,10,event)" onkeypress="return maskKeyPress(event)" class="form-control produtoCounter valorunitario" Size="5" />\
<input type="text" name="desconto' + x + '" id="desconto' + x + '" value="0" class="form-control produtoCounter desconto" Size="2" onchange="somarTotais()" placeholder="Desc" />\
<input type="text" name="estoque' + x + '" id="estoque' + x + '" class="form-control produtoCounter estoque" size="6" placeholder="Estoque" readonly />\
<input type="text" name="desconto1' + x + '" id="desconto1' + x + '" class="form-control produtoCounter desconto1" Size="2" onchange="somarTotais()" style="display: none;" />\
<a href="#" class="remove-camera">Remover</a>\
</div>');
$("#nome" + x).autocomplete({
source: "produtos.php",
minLength: 2,
select: function (event, ui) {
event.preventDefault();
$('#idproduto' + x).val(ui.item.idproduto);
idproduto = ui.item.idproduto;
$('#nome' + x).val(ui.item.nome);
produto = ui.item.nome;
$('#ean' + x).val(ui.item.codigo);
$('#referencia' + x).val(ui.item.referencia);
$('#valorunitario' + x).val(ui.item.valorunitario);
}
});
// verificar codigo de barras
$(function () {
$("#ean" + x).bind("change paste keyup", function() {
var total = $(this).val().length;
if(total == 13){
$(this).blur();
$('#idproduto' + x).val(ui.item.idproduto);
$('#nome' + x).val(ui.item.nome);
$('#ean' + x).val(ui.item.codigo);
$('#referencia' + x).val(ui.item.referencia);
$('#valorunitario' + x).val(ui.item.valorunitario);
$('#desconto1' + x).val(ui.item.desconto);
$('#estoque' + x).val(ui.item.quantidade);
$('#minimo' + x).val(ui.item.minimo);
}
}).blur(function() {
var total = $(this).val().length;
if(total == 13){
$('#idproduto' + x).val(ui.item.idproduto);
$('#nome' + x).val(ui.item.nome);
$('#ean' + x).val(ui.item.codigo);
$('#referencia' + x).val(ui.item.referencia);
$('#valorunitario' + x).val(ui.item.valorunitario);
$('#desconto1' + x).val(ui.item.desconto);
$('#estoque' + x).val(ui.item.quantidade);
$('#minimo' + x).val(ui.item.minimo);
}
}).
autocomplete({
autoFocus: true,
source: "codigos.php"
});
});
//fim da procura codigos de barras
}
});
$(wrapper).on("click", ".remove-camera", function (e) { //user click on remove text
e.preventDefault();
$(this).parent('div').remove();
somarTotais();
document.getElementById("enviar").disabled = false; // Habilitar
x--; //text box decrement
})
});
//Busca BD
$return_arr = array();
if ($con) {
$fetch = mysqli_query($con, "SELECT * FROM produtos where ean = $termo AND produtos.ativo = 1 ORDER BY idproduto DESC LIMIT 0 ,50");
while ($row = mysqli_fetch_array($fetch)) {
$row_array['nome'] = utf8_encode($row['nome']);
$idproduto = $row['idproduto'];
$row['nome'] = utf8_encode($row['nome']);
array_push($return_arr, $row_array);
}
}
mysqli_close($con);
echo json_encode($return_arr);
Can explain better the existence of the fields above the
autocomplete
? If that’s what I’m thinking, there’s a simpler way to do it, but I need more information. I suppose when entering the code you want the fields#idproduto
or#valorunitario
, for example, be automatically filled in, would that be?– Gnomo Escalate
I added the rest of the code.
– Fernando Trilha
Can the user search by product name or barcode? Or does the user first need to search for the name and then search for the barcode? I am asking pq in one part you search by in the name of the product and in another you search the CB, it was a little redundant, because the two functions fill the same fields, maybe you can validate the CB with a get inside select when searching for the name.
– Gnomo Escalate
Yes, he can search either by name or by code, but I could not get him to do the search in the same field, searching either by name or by code, because of that, I separated the search into two fields.
– Fernando Trilha
To search for both in the same field you have to tinker with your query that searches in the database that returns the products. What bank are you using? I’m asking these questions because your code has a good idea, but it has become complex to understand and I believe it can be simplified.
– Gnomo Escalate
I believe I can, but I confess that I don’t have the desired domain yet, despite trying hard and doing a lot of reading. I use mysql .
– Fernando Trilha
Just give me a minute that I put two possible solutions for you and we’ll exchange ideas!
– Gnomo Escalate
The idea is this, I read the barcode, it looks in the BD, if it finds it fills the other fields automatically. But the product may not have a registered barcode, so the user could search by name.
– Fernando Trilha