set the width of select2 input (through Angular-ui directive)
Asked Answered
D

15

174

I have problem making this plunkr (select2 + angulat-ui) work.

http://plnkr.co/edit/NkdWUO?p=preview

In local setup, I get the select2 work, but I cannot set the width as described in the docs. It is too narrow to be used.

enter image description here

Thank you.

EDIT: Never mind that plnkr, I found a working fiddle here http://jsfiddle.net/pEFy6/

It looks like it is the behavior of select2 to collapse to the width of first element. I could set the width through bootstrap class="input-medium" .Still not sure why angular-ui doesn't take config parameters.

Dentist answered 2/10, 2012 at 2:48 Comment(1)
Can you be more specific as far as what you've tried already? The chosen library (on which select2 is based) will generate a width based on the width provided in the HTML/CSS.Van
F
230

You need to specify the attribute width to resolve in order to preserve element width

$(document).ready(function() { 
    $("#myselect").select2({ width: 'resolve' });           
});
Flare answered 24/11, 2012 at 16:54 Comment(2)
$("#myselect").select2({ placeholder: 'Select Country', width: '192px' }); solved my problem with IE and have no effect on FF and Chrome! Thanks!Nonpayment
yes. I also asked author github.com/godbasin/vue-select2/issues/11East
F
285

In my case the select2 would open correctly if there was zero or more pills.

But if there was one or more pills, and I deleted them all, it would shrink to the smallest width. My solution was simply:

$("#myselect").select2({ width: '100%' });      
Fiend answered 18/1, 2014 at 1:26 Comment(5)
best answer if using select2 version 4.0.0Ouse
why this is better than $("#myselect").select2({ width: 'resolve' }); ? (seems to be the same)Foah
@RicardoVigatti: width:'resolve' does only work once on load of the page but not on resize. If your'e using responsive designs it doesnt do the trickSera
resolve didn't work for me - 100%` did. that's half an hour hopelessly searching for an answer "resolved". thanks :)Lancewood
This is the best answer so far. But now, for me, the search field doesn't fill all the width. Any easy way to correct this?Lugworm
F
230

You need to specify the attribute width to resolve in order to preserve element width

$(document).ready(function() { 
    $("#myselect").select2({ width: 'resolve' });           
});
Flare answered 24/11, 2012 at 16:54 Comment(2)
$("#myselect").select2({ placeholder: 'Select Country', width: '192px' }); solved my problem with IE and have no effect on FF and Chrome! Thanks!Nonpayment
yes. I also asked author github.com/godbasin/vue-select2/issues/11East
C
56

This is an old question but new info is still worth posting...

Starting with Select2 version 3.4.0 there is an attribute dropdownAutoWidth which solves the problem and handles all the odd cases. Note it is not on by default. It resizes dynamically as the user makes selections, it adds width for allowClear if that attribute is used, and it handles placeholder text properly too.

$("#some_select_id").select2({
    dropdownAutoWidth : true
});
Childe answered 1/6, 2014 at 11:53 Comment(1)
It works pretty well... except if you are using placeholders, if you are, if your options are smaller than the placeholder, the placeholder text will be truncated :(Lorenza
A
25

select2 V4.0.3

<select class="smartsearch" name="search" id="search" style="width:100%;"></select>
Antonetta answered 20/1, 2017 at 13:10 Comment(0)
I
15

With > 4.0 of select2 I am using

$("select").select2({
  dropdownAutoWidth: true
});

These others did not work:

  • dropdownCssClass: 'bigdrop'
  • width: '100%'
  • width: 'resolve'
Improve answered 2/5, 2017 at 5:46 Comment(0)
L
10

add method container css in your script like this :

$("#your_select_id").select2({
      containerCss : {"display":"block"}
});

it will set your select's width same as width your div.

Lower answered 21/2, 2014 at 12:30 Comment(2)
This did the trick for me (I couldn't get select2 to automatically resize with Bootstrap 3). Although I have to say it's a bit hacky.Antwanantwerp
This set me on the right track, I replaced display with minWidth: $( '.opt-option-type-select' ).select2( { containerCss : { minWidth: '10em', }, } );Pliable
I
7

Add width resolve option to your select2 function

$(document).ready(function() { 
        $("#myselect").select2({ width: 'resolve' });           
});

After add below CSS to your stylesheet

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

It will sort the issue

Idiomorphic answered 20/6, 2019 at 5:48 Comment(0)
H
2

Setting width to 'resolve' didn't work for me. Instead, i added "width:100%" to my select, and modified the css like this :

.select2-offscreen {position: fixed !important;}

This was the only solution that worked for me (my version is 2.2.1) Your select will take all the available place, it is better than using

class="input-medium"

from bootstrap, because the select is always staying in the container, even after resizing the window (responsive)

Hub answered 20/8, 2013 at 14:48 Comment(1)
Thanks for the solution but it only worked for me on first load. Once I selected a tag and then clicked on another page element the select2 input went back to the incorrect sizeForetoken
S
2

Wanted to add my solution here as well. This is similar to Ravindu's answer above.

I added width: 'resolve', as a property within the select2:

    $('#searchFilter').select2({
        width: 'resolve',
    });

Then I added a min-width property to select2-container with !important:

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

On the select element, the style='100px !important;' attribute needed !important to work:

<select class="form-control" style="width: 160px;" id="searchFilter" name="searchfilter[]">
Splice answered 10/11, 2020 at 16:18 Comment(0)
M
2

Add this in your stylesheet:

.select2 {
  width: unset !important;
}
Metalanguage answered 16/3, 2021 at 14:49 Comment(0)
M
1

Other Way you can use is setting width in style on your Select tag.

<select id="myselect" style="width: 20%;">
    <option>...</option>
</select>

Then, use this

$(document).ready(function() { 
    $("#myselect").select2({ width: 'style' }); //This will use style attr of the select element  
});
Manciple answered 22/3, 2021 at 9:36 Comment(0)
R
0

You can try like this. It works for me

$("#MISSION_ID").select2();

On hide/show or ajax request, we have to reinitialize the select2 plugin

For example:

$("#offialNumberArea").show();
$("#eventNameArea").hide();

$('.selectCriteria').change(function(){
    var thisVal = $(this).val();
    if(thisVal == 'sailor'){
        $("#offialNumberArea").show();
        $("#eventNameArea").hide();
    }
    else
    {
        $("#offialNumberArea").hide();
        $("#eventNameArea").show();
        $("#MISSION_ID").select2();
    }
});
Refinery answered 1/1, 2019 at 5:11 Comment(0)
N
0

Easier CSS solution independent from select2

//HTML
<select id="" class="input-xlg">
</select>
<input type="text" id="" name="" value="" class="input-lg" />

//CSS
.input-xxsm {
  width: 40px!important; //for 2 digits 
}

.input-xsm {
  width: 60px!important; //for 4 digits 
}

.input-sm {
  width: 100px!important; //for short options   
}

.input-md {
  width: 160px!important; //for medium long options 
}

.input-lg {
  width: 220px!important; //for long options    
}

.input-xlg {
  width: 300px!important; //for very long options   
}

.input-xxlg {
  width: 100%!important; //100% of parent   
}
Nonpayment answered 25/1, 2019 at 19:20 Comment(0)
D
0

On a recent project built using Bootstrap 4, I had tried all of the above methods but nothing worked. My approach was by editing the library CSS using jQuery to get 100% on the table.

 // * Select2 4.0.7

$('.select2-multiple').select2({
    // theme: 'bootstrap4', //Doesn't work
    // width:'100%', //Doesn't work
    width: 'resolve'
});

//The Fix
$('.select2-w-100').parent().find('span')
    .removeClass('select2-container')
    .css("width", "100%")
    .css("flex-grow", "1")
    .css("box-sizing", "border-box")
    .css("display", "inline-block")
    .css("margin", "0")
    .css("position", "relative")
    .css("vertical-align", "middle")

Working Demo

$('.select2-multiple').select2({
        // theme: 'bootstrap4', //Doesn't work
        // width:'100%',//Doens't work
        width: 'resolve'
    });
    //Fix the above style width:100%
    $('.select2-w-100').parent().find('span')
        .removeClass('select2-container')
        .css("width", "100%")
        .css("flex-grow", "1")
        .css("box-sizing", "border-box")
        .css("display", "inline-block")
        .css("margin", "0")
        .css("position", "relative")
        .css("vertical-align", "middle")
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.7/css/select2.min.css" rel="stylesheet" />

<div class="table-responsive">
    <table class="table">
        <thead>
        <tr>
            <th scope="col" class="w-50">#</th>
            <th scope="col" class="w-50">Trade Zones</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>
                1
            </td>
            <td>
                <select class="form-control select2-multiple select2-w-100" name="sellingFees[]"
                        multiple="multiple">
                    <option value="1">One</option>
                    <option value="1">Two</option>
                    <option value="1">Three</option>
                    <option value="1">Okay</option>
                </select>
            </td>
        </tr>
        </tbody>
    </table>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.7/js/select2.min.js"></script>
Deicer answered 17/7, 2019 at 18:5 Comment(0)
F
0

This is work for me. After create select componant and then add your customer css attribute.

$(select).select2({
//....create select componant content
});

$("span.select2-container").addClass(yourDefClass);

In the css file. configure your style and add !important.

.yourDefClass{
    width: 700px !important;
}
Fritter answered 13/7, 2022 at 2:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.