Select2 not calculating `resolved` width correctly if select is hidden
Asked Answered
T

14

50

I am using Twitter bootstrap and the fantastic Select2 plugin.

These are working great, I realized you need to set {width: 'resolve'} when initiating Select2 otherwise it looks messed up!.

But I am having an issue with one of my selects, as you can see in the image below, the Referee Type select has an incorrect width.

This is caused due to this field being initially hidden, and only becoming visible if Referee is selected in the Group field.

So, how can I fix this?

Inputs

Tieback answered 14/1, 2013 at 3:29 Comment(4)
Can you please provide a jsfiddle this way we can take a look at your problem?Cupped
one option is wait until element shown to initialize plugin, another is don't hide select but place it offscreen until needed. Demo would helpLefevre
I've been using select2 for a while and I usually set the width inline in the element style in the HTML - I know it is not very good practice but Select2 will attempt to copy the element style's width prior to resolving its actual dimensions so it is worth a shot. In that case you can remove width: 'resolve'.Prying
try adding inline width with "!important".Bijouterie
D
37

Select 2 is smart enough to recalculate it's size if only you set it in the select tag. Like so:

<select style="width: 100%"></select>

CSS formatting on classes would not work!

Read more in "Responsive design - Percent width"

Decry answered 14/1, 2016 at 1:41 Comment(4)
In addition to saying that this is the only correct answer, I feel like adding that the behavior is different between Firefox and Chrome for some reason. Kudos anyway!Prefatory
You are very welcome. Did you notice any difference between Firefox and Chrome?Decry
This definitely works, but may I know what makes this issue?Haley
This works for the select, but not for the placeholder inside. If there is a long placeholder, it gets cut offLehmann
O
20

You can set the width in the <select> element like so:

<select style="width:260px">

Until you find the desired width that you're looking for. In fact, the examples on the official select2 plugin site are done in this way.

However, if you're intent upon staying away from inline styles, you can always simply override select2's CSS and target the resulting container.

.select2-container {
  width: 260px;
}
Odrick answered 19/4, 2013 at 5:20 Comment(1)
omg this was driving me crazy, ugh the simplest things are always overlooked. Thanks for your answer.Hundley
H
20

I solved my similar problem with simple CSS:

.select2-container {
    width: 100% !important;
}
Harry answered 2/8, 2016 at 9:53 Comment(4)
This is by far the best answer. Easiest to implement also.Heteroplasty
It works for all cases (if element is hidden within a hidden parent and also if the element itself is hidden). No need to check any external cases.Iphlgenia
This interferes with other select boxes negativley in my instanceDentifrice
in my case, this causes me a vertical scroll because the span is not in the very left of the monitor and it's taking 100% of the <body> width which causes an overflow width –Ranch
S
8

Instead of invoking select2() on the Referee Type when the page loads, invoke select2 after showing Referee Type for the first time. This way, the select will be visible (with a proper width), and the 'width': 'resolve' option should work. If you provide a small jsfiddle example it would be easier to point this out.

Squeaky answered 19/7, 2013 at 9:22 Comment(2)
Does the tricks for ajax render (without quote for width). Thanks.Intercollegiate
THIS IS THE RIGHT ANSWER! :) My Select2 was working properly till I don't move to a hidden TAB. From that point width of Select2 was reduced to the selected value. This is just a BUG, the workaround is to display:block/declare Select2 /display:none during page ready.Mister
E
6

Set the style property of select element with width 100%

<select class="js-example-responsive" style="width: 100%"></select>

See Responsive design - Percent width

Essene answered 29/1, 2015 at 3:14 Comment(0)
F
3
.select2-container {
  width: 100% !important;
}
Fore answered 17/12, 2018 at 6:31 Comment(0)
S
2

Execute this when you unhide controls with Select2 attached:

//refresh select2 controls (to correct offscreen size bad calculation)
$("SELECT.select2-offscreen").select2();
Sooth answered 10/1, 2014 at 9:50 Comment(0)
P
2

You could bind a click event to the element that opens you hidden element and set the select2 width there. Worked for me and it's pretty simple. I used the click event because doing it with just 'document ready' wasn't working.

$('#click-me').on('click', function(event) { 
   $('.select2').select2({width: '100%'});
 }
Partiality answered 17/5, 2018 at 12:44 Comment(0)
S
1

According to documentation, you could add the proper width to settings on initialization:

$(".js-example-responsive").select2({
    width: '100%' // or 'resolve' or 'style' or 'element' - see docs
});

Worked like a charm for me while other recipes didn't help.

Silurid answered 21/1, 2022 at 7:14 Comment(0)
P
0

My preferred solution is to stop select2 from assigning those width and let them be automatic. Comment the following in select2.full.js :

For Container:

  Select2.prototype._placeContainer = function ($container) {
    $container.insertAfter(this.$element);

      //##!!
      /*
    var width = this._resolveWidth(this.$element, this.options.get('width'));

    if (width != null) {
      $container.css('width', width);
    }*/
  };

For Search inside the container:

  Search.prototype.resizeSearch = function () {

    //##!!
    /*
    this.$search.css('width', '25px');

    var width = '';

    if (this.$search.attr('placeholder') !== '') {
      width = this.$selection.find('.select2-selection__rendered').innerWidth();
    } else {
      var minimumWidth = this.$search.val().length + 1;

      width = (minimumWidth * 0.75) + 'em';
    }

    this.$search.css('width', width);
    */
  };

  return Search;
});
Placencia answered 11/12, 2015 at 5:42 Comment(2)
There's an open issue to fix this here, though at present no patch or response from the maintainer: github.com/select2/select2/issues/4513Instrumentalist
$('select').select2({ width: 'auto' });Mesencephalon
D
-1

hack to make it work is to run:

$('.filter-modal select').css('width', '100%'); before $('select').select2().

https://github.com/select2/select2/issues/4269

Dentifrice answered 21/8, 2017 at 2:57 Comment(0)
G
-1

This worked for me

.select2-container  {
    width: 100% !important;
}

also a fixed width

.select2-container  {
    width: 300px !important;
}
Gillam answered 13/4, 2018 at 15:36 Comment(0)
M
-1

Select2 has a strange bug which not "resolve" in the correct way hidden Select2 elements. It's not working place width: "resolve" as this is already the default value. Instead the workaround is to show the select2 and hide them suddenly a second later the declaration.

$("#div_container_of_select2").show(); //make it visible for a millisecond
$("select[name=referee_type]").select2(); //declare Select2
$("#div_container_of_select2").hide(); //back to darkness

This will render correctly the select2.

Mister answered 18/5, 2018 at 8:27 Comment(0)
A
-2
$("span.select2").css("width", "100%");
Audraaudras answered 9/6, 2016 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.