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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么