oligan
oligan

Reputation: 634

jquery autocomplete & symfony2

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 !

Upvotes: 0

Views: 2201

Answers (1)

Chase
Chase

Reputation: 9362

You can change your controller to get the term. Keep in mind that by default autocomplete expects EITHER an array containing single terms. Or and Array of objects with label and value keys.

public function AutoCompletePaysAction(Request $request)
{
    $term = $request->get('term',null)
    $repository = $this->getDoctrine()->getManager()->getRepository('MyAwesomeWebsiteHomeBundle:Countries');
    if($term){
        $countries = $repository->createQueryBuilder('c')
            ->where("c.countryName LIKE '%:term%'")
            ->setParameter('term', $term)
            ->getQuery()
            ->getResult();
    }
    else{
        $countries = $repository->findAll();
    }
    $list = array();
    foreach($countries as $country){
        array_push($list, array('label'=>$country->getCountryName(), 'value'=>$country->getCountryName());
    }
    return new JsonResponse($list,200);
}

Upvotes: 2

Related Questions