DragonWar% 2014-10-29 14:18 采纳率: 0%
浏览 27

jQuery的自动完成和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 !

  • 写回答

1条回答 默认 最新

  • weixin_33682719 2014-10-29 19:32
    关注

    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);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀