Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select2 doesn't work when embedded in a bootstrap modal

When I use a select2 (input) in bootstrap modal, I can't type anything into it. It's like disabled? Outside the modal select2 works fine.

enter image description here

Working example: http://jsfiddle.net/byJy8/1/ code:

<!-- Modal --> <div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">     <div class="modal-header">         <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>         <h3 id="myModalLabel">Panel</h3>     </div>     <div class="modal-body" style="max-height: 800px">                 <form class="form-horizontal">         <!-- Text input-->         <div class="control-group">             <label class="control-label" for="vdn_number">Numer</label>             <div class="controls">                 <!-- seleect2 -->                 <input name="vdn_number" type="hidden" id="vdn_number"  class="input-large" required=""  />             </div>         </div>       </form>         </div>     <div class="modal-footer">         <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>         <button class="btn btn-primary">Save changes</button>     </div> </div> 

JS

$("#vdn_number").select2({     placeholder: "00000",     minimumInputLength: 2,     ajax: {         url: "getAjaxData/",         dataType: 'json',         type: "POST",         data: function (term, page) {             return {                 q: term, // search term                 col: 'vdn'             };         },         results: function (data) { // parse the results into the format expected by Select2.             // since we are using custom formatting functions we do not need to alter remote JSON data             return {results: data};         }     } }); 

answers:

here you can find a quick fix

and here is 'the right way': Select2 doesn't work when embedded in a bootstrap modal

like image 382
breq Avatar asked Aug 28 '13 11:08

breq


People also ask

How do I create a select2 dynamically?

Select2 jQuery plugin is easy to add on the <select > element. Need to call the select2() method on the selector to initialize. If you adding select2 on a class or select element and when you add an element dynamically then select2 is not initialized on that element.

What is dropdownParent?

The dropdownParent option allows you to pick an alternative element for the dropdown to be appended to: $('#mySelect2'). select2({ dropdownParent: $('#myModal') }); This is useful when attempting to render Select2 correctly inside of modals and other small containers.

How do I know if select2 is open?

select2:open is fired whenever the dropdown is opened. select2:opening is fired before this and can be prevented. select2:close is fired whenever the dropdown is closed. select2:closing is fired before this and can be prevented.

How does select2 search work?

When users filter down the results by entering search terms into the search box, Select2 uses an internal "matcher" to match search terms to results. You may customize the way that Select2 matches search terms by specifying a callback for the matcher configuration option.


2 Answers

Ok, I've got it to work.

change

<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">     <div class="modal-header">         <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>         <h3 id="myModalLabel">Panel</h3>     </div>     <div class="modal-body" style="max-height: 800px"> 

to

<div id="myModal" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">     <div class="modal-header">         <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>         <h3 id="myModalLabel">Panel</h3>     </div>     <div class="modal-body" style="max-height: 800px"> 

(remove tabindex="-1" from modal)

like image 91
breq Avatar answered Sep 18 '22 11:09

breq


For Select2 v4:

Use dropdownParent to attach the dropdown to the modal dialog, rather than the HTML body.

<!-- Modal --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">   <div class="modal-dialog" role="document">     <div class="modal-content">       <div class="modal-header">         <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>         <h4 class="modal-title" id="myModalLabel">Modal title</h4>       </div>       <div class="modal-body">         <select id="select2insidemodal" multiple="multiple">           <option value="AL">Alabama</option>             ...           <option value="WY">Wyoming</option>         </select>       </div>       <div class="modal-footer">         <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>         <button type="button" class="btn btn-primary">Save changes</button>       </div>     </div>   </div> </div>   <script>  $(document).ready(function() {   $("#select2insidemodal").select2({     dropdownParent: $("#myModal")   }); });  </script> 

This will attach the Select2 dropdown so it falls within the DOM of the modal rather than to the HTML body (the default). See https://select2.org/dropdown#dropdown-placement

like image 41
dboswell Avatar answered Sep 18 '22 11:09

dboswell