So i want to display this list of countries on an input : I use Jquery UI Autocomplete with an ajax call to the db.
Javascript :
$("#fos_user_registration_form_country").autocomplete({
minLength: 2,
scrollHeight: 220,
source: function(req, add){
$.ajax({
url:Routing.generate('user_register_countries_autocomplete'),
type:"get",
dataType: 'json',
data: 'title_search='+req.term,
async: true,
cache: true,
success: function(data){
var suggestions = [];
//process response
$.each(data, function(i, val){
suggestions.push({"name": val.countryName});
});
//pass array to callback
add(suggestions);
}
});
}
});
Controller :
public function AutoCompletePaysAction()
{
$repository = $this->getDoctrine()->getManager()->getRepository('MyAwesomeWebsiteHomeBundle:Countries');
$listeCountries = $repository->countriesArray();
$liste = json_encode($listeCountries);
return new Response($liste);
}
repo
public function countriesArray()
{
// $query = $this->createQueryBuilder('s');
$query = $this->_em->createQuery("SELECT a.countryName FROM MyAwesomeWebsiteHomeBundle:Countries a");
return $query->getArrayResult();
}
what shows in firebug ( response of my ajax call = all the countries )
[{"countryName":"United States"},{"countryName":"Canada"},{"countryName":"Afghanistan"},{"countryName":"Albania"},...
-> The ajax call seems to work fine each time i type a letter in the input, but no suggestions appears as it should do with autocomplete.
Also it seems weird to me to make an ajax call each time a letter is typed to retrieve the same values, but well thats how its done in the doc. Also i tried to preload the array but i just can't make it work. tldr : lost.
Any suggestions ? Thanks !