我将向您展示如何集成Select2
到 Symfony3 项目中!
首先,将这 3 个链接添加到base.html.twig
页面。
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/css/select2.min.css" rel="stylesheet" />
<link href ="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.min.js" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/js/select2.min.js"></script>
二、CompanyType.php
补充:
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
class CompanyType extends AbstractType {
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('country',EntityType::class, array(
'attr'=>array('class'=>"country",),
'label'=>'Country of The Head fice',
'choices_as_values' => true, //
'class' => 'QSCORBundle\Entity\CountryMaps',
'choice_label' => 'Country_Name',
))
->add(...)
}
...
}
之后,在你的实体文件上Country.php
添加:
public function __toString()
{
// TODO: Implement __toString() method.
return $this->getCountryName();
}
最后,在你的模板文件中xxx.html.twig
写下:
<script type="text/javascript">
$(document).ready(function() {
$(".country").select2();
});
</script>
{{ form_widget(form.country)}}
这是它的外观图像: