在symfony2中,JQuery Autocomplete表现不佳

时间:2015-10-02 07:28:44

标签: symfony autocomplete jquery-ui-autocomplete

我在symfony2中使用自动完成时遇到问题。 在这里我使用autocomplete jquery UI库,我必须将它包含在我的twig页面上。

但是当我尝试运行此自动完成时,当我在文本输入中输入内容时,没有任何内容出现。

我曾尝试在Google上寻找解决方案,但仍然没有成功。 这是我在symfony2中的php编码:

$students = $this->getDoctrine()
->getRepository('DSBBundle:Student')
->findAll();

$key=1;
$student_name = array();
foreach ($students as $student)
{
  $student_name[$key] = '"'.$student->getName().'",';
  $key++;
}

这是表单构建器中的语法:

->add('student', 'text', array(
                    'label' => 'NID Siswa',
                    'attr'   =>  array('class'=>'student')

这是我的树枝中的javascript语法:

<script>
  var nama = [
  {% for key, student in students %}
      {{ student }}
  {% endfor %}
  ];


  $( ".student" ).autocomplete({
    source: nama
  });
</script> 

请帮助我,谢谢:)

2 个答案:

答案 0 :(得分:0)

如果我理解你的话:

1)你应该更好地进行第一次迭代:

0

看起来更好吗?

2)你有没有插入$student_name = array(); foreach ($students as $key => $student) { $student_name[$key] = '"'.$student->getName().'",'; } 阵列?这是什么?

nama

看看this post,它可能会帮助您转换任何额外的数据。

希望这会对你有所帮助:)

答案 1 :(得分:0)

您应该使用Data transformer

在你的情况下,看看this issue它帮助了我