简体   繁体   中英

Symfony 4 - Use bootstrap 4 with Select2

in my symfony 4 project I use Select2 for some of my fields: https://select2.org/

So, I've this:

在此处输入图像描述

So, in my form, I've:

->add('user', EntityType::class, [
                'class' => User::class,
                'label' => 'Agent',
                'placeholder' => "Agent",
                'choice_label' => 'fullName',
                'choice_value' => 'fullName',
                'attr' => [
                    'class' => 'js-example-basic-single',
                ]
            ])

And in my view:

<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.8/css/select2.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.8/js/select2.min.js"></script>

<script>
        $('.js-example-basic-single').select2({
            placeholder: 'Choisissez un agent',
            allowClear: true,
            language: 'fr'
            });
    </script>

I would like to use bootstrap 4 with this field, so that it has the same design as the other fields.

So, I found this link: https://github.com/ttskch/select2-bootstrap4-theme Is it a good link?

It's the first time I use npm, so I executed the following command:

npm install @ttskch/select2-bootstrap4-theme

But I dont see where are the css links, to import them in my view

You just have to pass correct theme name in select2 field definition. So in your case it will look like this:

 <script>
        $('.js-example-basic-single').select2({
            placeholder: 'Choisissez un agent',
            allowClear: true,
            language: 'fr',
            theme: 'bootstrap4',
            });
    </script>

If you're using webpack as official documentation suggest, you should add following lines:

assets/js/app.js

require('select2');

assets/css/app.scss:

@import "~select2-bootstrap4-theme/dist/select2-bootstrap4.min.css";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM