samson/autocomplete-bundle

轻松将Select2自动完成小部件集成到您的Symfony应用程序中,以搜索Doctrine实体

2.0.3 2015-02-12 14:45 UTC

README

查看SamsonIT/ShowCase以了解如何使用此包的工作示例。

如何安装

目前,您需要遵循以下步骤以确保自动完成功能正常工作

  • 在您的AppKernel中启用此功能和UnexpectedResponseBundle

示例:[https://github.com/SamsonIT/ShowCase/blob/master/app/AppKernel.php#L22](https://github.com/SamsonIT/ShowCase/blob/master/app/AppKernel.php#L22)

  • 确保加载最新的JQuery版本(JQuery UI不是必需的)
  • 确保加载最新的Select2 JavaScript
  • 确保加载最新的Select2 CSS文件,并且与之捆绑的图像已就位
  • 确保加载此包中的autocomplete.js文件
  • 确保加载此包中的autocomplete.css文件(此文件并非必需)

上述要求的示例可以在[https://github.com/SamsonIT/ShowCase/blob/master/src/Samson/Bundle/ShowCaseBundle/Resources/views/Autocomplete/index.html.twig](https://github.com/SamsonIT/ShowCase/blob/master/src/Samson/Bundle/ShowCaseBundle/Resources/views/Autocomplete/index.html.twig)中找到

  • 确保将autocomplete.html.twig表单主题包含到twig中

在此处找到示例:[https://github.com/SamsonIT/ShowCase/blob/master/app/config/config.yml#L24](https://github.com/SamsonIT/ShowCase/blob/master/app/config/config.yml#L24)

如何使用

使用自动完成非常简单。配置表单类型需要三个必选选项和一个推荐选项

$form = $this->createForm('autocomplete', null, array(
  'class' => 'SomeEntityClass',
  'template' => 'Template to use to display the entity.html.twig',
  'search_fields' => array('list of fields to search in'),
  'query_builder' => function(EntityRepository $er) {
    return $er->createQueryBuilder('s');
  }
);

模板可以定义为以下形式

{% if highlight %}
Found: {{ result.name|highlight }}
{% else %}
{{ result.name }}
{% endif %}

如果highlight属性为false,则highlight过滤器将简单地不执行任何操作,因此这也是一个有效的模板

{{ result.name|highlight }}

升级

查看Upgrade.md