openforce/select2-bundle
最新稳定版本:v1.1.1
Composer 安装命令:
composer require openforce/select2-bundle
包简介
symfony select2 entity form type
README 文档
README
Introduction
This is a symfony Entity Form Type that supports select2 without using a controller. It's so easy and simple.
Installation
Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.
Step 1: Download the Bundle
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ composer require openforce/select2-bundle
Step 2: Enable the Bundle
Then, enable the bundle by adding it to the list of registered bundles
in the config/bundles.php file of your project:
// config/bundles.php return [ // ... \Openforce\Select2Bundle\OpenforceSelect2Bundle::class => ['all' => true], ];
Step 3: Configure twig.yaml file
twig: form_themes: - "@OpenforceSelect2/select2_type.html.twig"
Step 4: Add select2 to your template
//Add your template file <script src="//code.jquery.com/jquery-1.9.0rc1.js"></script> <link href="//cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" /> <script src="//cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
How to use
The main options are:
classRequired. Your target entity.search_fieldRequired. Entity field name you want to use for your search.choice_labelOptional. Entity field name you want to display on the label. default value is__toString.max_resultsOptional. Number to display on page.filterOptional. It is callback function. If you use this, you don't need to definesearch_field.related_fieldsOptional. This option allows you to use the values you enter in other fields with thefilteroption.
// your Controller file public function index(){ $formBuildr = $this->createFormBuilder(); $formBuilder->add('price', InputType::class); $formBuilder->add("product", Select2Type::class,[ 'class' => Product::class, 'search_field' => 'name', 'choice_label' => 'name', 'max_results' => 50, 'related_fields' => ['price'], 'filter' => function(EntityRepository $er, $value, $relatedFields){ return $er->createQueryBuilder("p") ->where("p.description like :word and p.price < :price") ->setParameter("word", "%".$value."%") ->setParameter('price', $relatedFields['price']) ->orderBy("p.productId") ; } ]) }
统计信息
- 总下载量: 771
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-08-12