jQuery autocomplete shows no options

Asked

Viewed 2,397 times

6

Description


Need: I need to make an autocomplete with jQuery, PHP and from what was selected, caught the ID, I make a query in the database and display what I need.


Problem: I’m having trouble displaying the records on input which loads the autocomplete. Note the code below Javascript.


Javascript (jQuery)

$( "#consulta_estoque" ).autocomplete({
  source: 'js/autocomplete.php',
  select: function(event, ui){
      $( "#consulta_estoque" ).val(ui.item.descricaoProduto);
      alert(ui.item.codigoFabrica);
  }
});

PHP


foreach ($sql as $res) {
    $resultado[] = $res;
}
echo json_encode($resultado);

JSON


[
{
"codProduto":"9",
"codigoFabrica":"8019077",
"codSubcategoria":"0",
"descricaoProduto":"WWAKS3-5\/S366 CONECTOR M12 EUROFAST ANGULAR FEMEA",
"tipoItemEstoque":"0",
"c7flex":"9",
"ncmProduto":"85444200"
},{
"codProduto":"39",
"codigoFabrica":"8019078",
"codSubcategoria":"0",
"descricaoProduto":"WWAKS3-10\/S366 CONECTOR M12 EUROFAST ANGULAR MACHO",
"tipoItemEstoque":"0",
"c7flex":"39",
"ncmProduto":"0"
}
]

Observing: I believe that the JSON that is correct.

Can someone help me with that?

  • 1

    From what I see, your JSON is an Array, so it wouldn’t be right to access: ui.item[0].descricaoProduto instead of ui.item.descricaoProduto? I would like you to make available the entire PHP so that I can include it in http://jsfiddle.net to make a functional example

  • So friend, the whole php is complicated, because there are many classes to provide

  • The problem is that I have no way to test... so test the following to see if it works: $( "#consulta_estoque" ).val(ui.item[0].descricaoProduto); and alert(ui.item[0].codigoFabrica);

  • Open the browser console (F12) and see if you have any error popping up.

  • 2

    PHP seems to be correct because JSON is right. By plugin documentation the use also seems certain.

  • Yeah, and if I do like this: $result[] = $res['descricaoProduct']; Shows right, but I can’t get the id that was clicked

  • If you do as JSON does?

  • ["WWAKS3-5/S366 EUROFAST ANGULAR FEMEA M12 CONNECTOR","WWAKS3-10/S366 EUROFAST ANGULAR MALE M12 CONNECTOR"]

  • Thanks for the @Gustavorodrigues documentation link was very helpful to develop the reply.

  • None of the answers answers your question?

Show 5 more comments

3 answers

2

According to the plugin documentation, you need 2 fields (I assume mandatory) in the JSON object:

An array of Objects with label and value properties:
Translated: An array of objects with properties label and value

So your JSON has to be changed/completed to be, for example, like:

var json = [{
    "label": "EUROFAST ANGULAR FEMEA",
        "value": "8019077",
        "codProduto": "9",
        "codigoFabrica": "8019077",
        "codSubcategoria": "0",
        "descricaoProduto": "WWAKS3-5\/S366 CONECTOR M12 EUROFAST ANGULAR FEMEA",
        "tipoItemEstoque": "0",
        "c7flex": "9",
        "ncmProduto": "85444200"
}, {
    "label": "EUROFAST ANGULAR MACHO",
        "value": "8019078",
        "codProduto": "39",
        "codigoFabrica": "8019078",
        "codSubcategoria": "0",
        "descricaoProduto": "WWAKS3-10\/S366 CONECTOR M12 EUROFAST ANGULAR MACHO",
        "tipoItemEstoque": "0",
        "c7flex": "39",
        "ncmProduto": "0"
}]

jsFiddle

Regarding the server-side part. I suggest that JSON be passed via ajax. Or before the autocomplete be attached to the input to the source is already assigned, or focus of input:

$("#consulta_estoque").on('focus', function(){
    json = json.concat(json2); // aqui seria um pedido ajax
    $("#consulta_estoque").autocomplete({source: json});
});

Example version using ajax, called when the focus shoots:

$.ajax({
    type: 'GET',
    url: 'js/autocomplete.php',
    dataType: 'jsonp',
    success: function (resposta) {
        var json = resposta;
        $("#consulta_estoque").autocomplete({source: json});
    }
});

jsFiddle with ajax (using the recently discovered jsontest.com)

  • 3

    i did not know this site also http://jsontest.com very good!

1

The problem is that the source: jQuery Autocomplete UI requires an array of strings and not a JSON, see how it would be functional:

First you’d have to put descricaoProduto in an array, and then arrow it as source:

$.getJSON('js/autocomplete.php').done(function (dados) {
  // Cria uma array adaptada ao plugin:
  var dadosAdaptados = $.map(dados, function (elemento) {
    return {
      label: elemento.descricaoProduto,
      value: elemento.codigoFabrica
    };
  });

  // Configura o autocomplete:
  $( "#consulta_estoque" ).autocomplete({
    source: dadosAdaptados,
    select: function (event, ui) {
      // Usa os dados conforme necessário:
      $( "#consulta_estoque" ).val(ui.item.label);
      alert('Código de fábrica:' + ui.item.value);
  }
});

This way it works, so you have to separate your JSON into an array.

Example in Jsfiddle

-2

The link below presents a great solution for what you need:

http://www.a2zwebhelp.com/bootstrap-autocomplete

The solution presented uses the Bootstrap interface. I hope it helps.

Browser other questions tagged

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