tetranz / select2entity-bundle

A Symfony2 bundle that integrates Select2 as a drop-in replacement for a standard entity field on a Symfony form.
MIT License
218 stars 110 forks source link

Bootstrap theme #28

Open bettinz opened 8 years ago

bettinz commented 8 years ago

Hello, I'm using your bundle and it works fine. The only problem is bootstrap theme: I'm using the official theme from https://github.com/select2/select2-bootstrap-theme but it doesn't work. It says I need to set theme: "bootstrap" when initialize select2. Can you help me to do that?

I've solved adding theme: "bootstrap",

in bundles/tetranzselect2entity/js/select2entity.js

$(document).ready(function () {
  $.fn.select2entity = function(action) {
    var action = action || {};
    this.select2($.extend(action, {
      theme: "bootstrap",
      ajax: {
        data: function (params) {
          return {
            q: params.term
          };
        },
        processResults: function (data) {
          return {
            results: data
          };
        }
      }
    }));
    return this;
  };

  $('.select2entity').select2entity();
});

do you have a more clean solution, like config.yml? Thanks

tetranz commented 8 years ago

You can set this with a data attribute.

In your template you can do something like this

{{ form_row(form.myfield, {'attr':{'data-theme':'bootstrap'}}) }}

on in the form type class when adding that field:

'attr' => [
  'data-theme' => 'bootstrap'
]

I agree that a default set in yml would be good. I'll look into that.

Thanks for pointing this out. I haven't actually used this for a long time and didn't know about the theme project. :) I'll have to update the docs because https://github.com/t0m/select2-bootstrap-css is obsolete now.

alexndlm commented 4 years ago

157

T1p0un3t commented 4 years ago

Hello,

select2 provide global configuration : $.fn.select2.defaults.set( "theme", "bootstrap" );

my 2c