定制 openforce/select2-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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:

  • class Required. Your target entity.
  • search_field Required. Entity field name you want to use for your search.
  • choice_label Optional. Entity field name you want to display on the label. default value is __toString.
  • max_results Optional. Number to display on page.
  • filter Optional. It is callback function. If you use this, you don't need to define search_field.
  • related_fields Optional. This option allows you to use the values you enter in other fields with the filter option.
// 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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-08-12