Da der Quellcode zu modifizieren ist keine Option, und das Hinzufügen eines Haken zum select2:open
Ereignis ist nicht sehr elegant, vor allem, wenn Sie mehrere select2 Instanzen Auf der gleichen Seite habe ich eine kleine Erweiterung für das Select2
Plugin geschrieben.
Meine Implementierung basiert auf einem PR aus dem Repository des Plugins (https://github.com/select2/select2/pull/4618), das noch nicht zusammengeführt wurde.
Grundsätzlich überschreibt der folgende Code die ursprüngliche Plugin-Funktion, die die Dropdown-Positionierung behandelt, und fügt eine neue Option (dropdownPosition
) hinzu, um die Dropdown-Positionierung über/unter zu erzwingen.
Die neue Option dropdownPosition
kann folgende Werte annehmen: - below
- Das Dropdown wird immer am unteren Rand der Eingabe angezeigt; - above
- das Dropdown wird immer am Anfang der Eingabe angezeigt; - auto
(Standard) - es verwendet das alte Verhalten.
Legen Sie einfach den folgenden Code nach select2.js
Datei:
(function($) {
var Defaults = $.fn.select2.amd.require('select2/defaults');
$.extend(Defaults.defaults, {
dropdownPosition: 'auto'
});
var AttachBody = $.fn.select2.amd.require('select2/dropdown/attachBody');
var _positionDropdown = AttachBody.prototype._positionDropdown;
AttachBody.prototype._positionDropdown = function() {
var $window = $(window);
var isCurrentlyAbove = this.$dropdown.hasClass('select2-dropdown--above');
var isCurrentlyBelow = this.$dropdown.hasClass('select2-dropdown--below');
var newDirection = null;
var offset = this.$container.offset();
offset.bottom = offset.top + this.$container.outerHeight(false);
var container = {
height: this.$container.outerHeight(false)
};
container.top = offset.top;
container.bottom = offset.top + container.height;
var dropdown = {
height: this.$dropdown.outerHeight(false)
};
var viewport = {
top: $window.scrollTop(),
bottom: $window.scrollTop() + $window.height()
};
var enoughRoomAbove = viewport.top < (offset.top - dropdown.height);
var enoughRoomBelow = viewport.bottom > (offset.bottom + dropdown.height);
var css = {
left: offset.left,
top: container.bottom
};
// Determine what the parent element is to use for calciulating the offset
var $offsetParent = this.$dropdownParent;
// For statically positoned elements, we need to get the element
// that is determining the offset
if ($offsetParent.css('position') === 'static') {
$offsetParent = $offsetParent.offsetParent();
}
var parentOffset = $offsetParent.offset();
css.top -= parentOffset.top
css.left -= parentOffset.left;
var dropdownPositionOption = this.options.get('dropdownPosition');
if (dropdownPositionOption === 'above' || dropdownPositionOption === 'below') {
newDirection = dropdownPositionOption;
} else {
if (!isCurrentlyAbove && !isCurrentlyBelow) {
newDirection = 'below';
}
if (!enoughRoomBelow && enoughRoomAbove && !isCurrentlyAbove) {
newDirection = 'above';
} else if (!enoughRoomAbove && enoughRoomBelow && isCurrentlyAbove) {
newDirection = 'below';
}
}
if (newDirection == 'above' ||
(isCurrentlyAbove && newDirection !== 'below')) {
css.top = container.top - parentOffset.top - dropdown.height;
}
if (newDirection != null) {
this.$dropdown
.removeClass('select2-dropdown--below select2-dropdown--above')
.addClass('select2-dropdown--' + newDirection);
this.$container
.removeClass('select2-container--below select2-container--above')
.addClass('select2-container--' + newDirection);
}
this.$dropdownContainer.css(css);
};
})(window.jQuery);
Das initialisieren das Plugin mit wie folgt:
$(document).ready(function() {
$(".select-el").select2({
dropdownPosition: 'below'
});
});
Fiddle hier: https://jsfiddle.net/byxj73ov/
Github-Repository: https://github.com/andreivictor/select2-dropdownPosition
Das scheint nicht für mich zu funktionieren: http://jsfiddle.net/brunodd/jEADR/2006/ – brunodd
Ich bin mir nicht sicher, was du meinst, es funktioniert ok für mich in Chrome – shanabus
Die Version, die ich benutze ist 4.0 und das ist noch 3. Ich habe es sowieso schon behoben. http://jsfiddle.net/brunodd/jEADR/2009 – brunodd