Auto complete of Jqueryui

Asked

Viewed 46 times

1

I’m not being able to list my Json in the jqueryui autocomplete. In the browser console I see that it is calling no more shows at the time I apply the append in LI.

$(function(){
    var mostraLista = "http://jsonplaceholder.typicode.com/users";
  		$.get(mostraLista, function(response){
		var dados = response;
		for(var i = 0; i < dados.length; i++){
			var tudoJ = dados[i];
			console.log(tudoJ.name);
			$("<li>").text(tudoJ.email).appendTo(".ui-corner-all");
		}
	});

  $("#tags").autocomplete({
  	source: mostraLista
  });	
});

1 answer

1


The autocomplete needs to have the data already formatted. If you give it an array of what you need it will already work. In the example below I mapped the data to create a name-only array. And so in the autocomplete it automatically fetches and inserts the required HTML.

$(function() {
    var url = "http://jsonplaceholder.typicode.com/users";
    $.get(url, function(response) {
        var nomes = response.map(function(pessoa){
        return pessoa.name;
        })
        $("#tags").autocomplete({
            source: nomes
        });
    });
});

jsFiddle: http://jsfiddle.net/zskqf7hj/

  • Solved my problem, but I was left with a doubt. as he made the append ?

  • @Diboa the plugin itself does this.

Browser other questions tagged

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