I want something like the first example here https://material.angular.io/components/select/overview
I tried all the methods provide here Select2 change container position .
They work , BUT the dropdown is closed automatically right after it opens!
This happening because the dropdown is also getting the click event because the dropdown is opening exactly under the mouse cursor !
I tested it with this
.on('select2:open', function (e) {
var container = $('.select2-container .select2-dropdown');
setTimeout(function () {
container.addClass('country-select-dropdown');
},1000);
})
If i delay the repositioning the dropdown won't close automatically.
I was thinking about implementing a custom dropdown adapter but that seems too complicated for a simple repositioning task!
SO any solution?
Also I am opento using other libraries ... I just need a custom select box ...
$(document).ready(function() {
$('.js-example-basic-single').select2({
dropdownAutoWidth: true,
minimumResultsForSearch: -1,
dropdownCssClass: 'country-select-dropdown',
dropdownParent: $('#slectParent').parent()
});
});
#slectParent {
position: reletive;
}
.country-select-dropdown {
top: -2rem;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>
<div id="slectParent">
<select class="js-example-basic-single" name="state">
<option value="AL">Alabama</option>
<option value="WY">Wyoming</option>
</select>
<div>
pointer-events: none;
supported in all browsers? – Prostatectomy