samson / autocomplete-bundle
轻松将Select2自动完成小部件集成到您的Symfony应用程序中,以搜索Doctrine实体
Requires
- php: >=5.3.0
- doctrine/orm: ~2.2
- samson/unexpected-response-bundle: ~0.1
- symfony/form: ~2.1
- symfony/http-kernel: ~2.1
- symfony/twig-bundle: ~2.1
Requires (Dev)
- symfony/symfony: ~2.1
This package is auto-updated.
Last update: 2024-09-20 21:38:41 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 }}