Symfony Ajax Autofill

时间:2015-11-19 15:02:47

标签: jquery json ajax symfony

我在设置自动填充表单(城市名称)时遇到了一些问题,我有响应JSON(在firebugs中检查)但我无法使自动完成jquery ui正常工作。

使用以下文件:

“”实体 - >产品和城市“”;

“”表格 - > ProductsType和CityType“”

有一种关系OneToOne产品 - >城市

来自控制器的请求:

/**
 * [citiesAction description]
 *
 * @Route("/vendre/{ville}", name="ville")
 * @Method("GET")
 */
public function citiesAction(Request $request, $ville)
{
        $em = $this->getDoctrine()->getManager();
        $cityName = $em->getRepository('ApxDevPagesBundle:City')->ajaxCity($ville);

        if($cityName)
        {
            $cities = array();
            foreach($cityName as $city)
            {
                $cities[] = $city->getNomCommune();
            }
        } else {
            $city = null;
        }

        $response = new JsonResponse();

        return $response->setData(array('ville' => $cities));


}

我的表格: (班级名称=> ville)

 <label>{{'product_city'|trans|capitalize }}</label>
                        {{ form_widget(form.city) }}

最后是我的ajax:

  $( ".ville" ).autocomplete({
  source: function( request, response ) {
    $.ajax({
      url: 'http://ptijobs.dev/app_dev.php/vendre/' + $('.ville').val(),
      dataType: "jsonp",
      data: {
        q: request.term
      },
      success: function( data ) {
        response ( data );
      }
    });
  }
});

如你所见,我得到了答复:

  

See JSON response

任何让它运作的指令都会很棒

1 个答案:

答案 0 :(得分:1)

$(function() {
function log( message ) {
  $( "<div>" ).text( message ).prependTo( "#log" );
  $( "#log" ).scrollTop( 0 );
}

$( ".ville" ).autocomplete({
  source: function( request, response ) {
    $.ajax({
      url: 'http://ptijobs.dev/app_dev.php/vendre/' + $(".ville").val(),
      dataType: "json",
      data: {
        q: request.term
      },
      success: function( data ) {
        response( data );
      }
    });
  },
  minLength: 3,
  select: function( event, ui ) {
    log( ui.item ?
      "Selected: " + ui.item.label :
      "Nothing selected, input was " + this.value);
  },
  open: function() {
    $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
  },
  close: function() {
    $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
  }
});

});