New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrong search box position when dropdown opens above #4614
Comments
I was able to fix something similar by doing this after initializing the select2 instance: var select2Instance = $(selectNode).data('select2');
select2Instance.on('results:message', function(params){
this.dropdown._resizeDropdown();
this.dropdown._positionDropdown();
}); In my case I had a multiple selection box whose dropdown was detaching much the same way that your box single selection results box was detaching. |
Many thanks for the fix! |
Has anyone submitted a PR for this as a proper fix? |
It would appear that this problem is not limited to AJAX-sourced dropdowns. It occurs any time there are "no results" after previously making some selections. |
There anybody actively working on this one? If not, anybody got ideas on where to start solving this issue? I'll make a PR if I can fix it, but I'd appreciate a starting point on where to look. |
@AStoker that would be great! You can check some of the duplicate issues listed above, which may have more insight or proposed solutions. |
Workaround that works with the latest version of select2 (and for us) was found here. |
Just for reference, that workaround is:
|
Clearing out the previous results could work, but on the other hand it could have other unintended side effects. I'd rather if someone dug into the code more thoroughly to find the root of this problem. |
What seems to be happening is that when the window is opened, any previous results are still in the DOM, so the search box appears correctly above where we'd normally expect. But then when Select2 goes in sees that the results should be different (empty), it clears the results, but doesn't reposition the drop down. So it appears to be a case of repositioning when clearing results (as it does reposition when the results are changing live). Hints of where that logic (clearing/restarting search on open) would reside? |
I have no idea - I'm not all that familiar with the codebase myself. |
So, I can see that when we get new results, we are repositioning the dropdown (line). So that leaves the two use cases that we have to fix.
In case 1, we start (more or less) at this this line. At this point, we're opening back up the search box (line 299) if it's not open yet. We haven't cleared out our previous results yet (that happens next), and that's why this fix with the open trigger works, we manually clear out the results and reposition the drop down. Continuing down this event chain, after we've triggered the Results.prototype.displayMessage = function (params) {
var escapeMarkup = this.options.get('escapeMarkup');
this.clear();
this.hideLoading();
this.data.container.dropdown._positionDropdown(); //This line added
... Functionally, that works, but conceptually, I don't like calling "hidden" functions (functions starting with underscore) in this way. Is there a more front facing function that could be called here? |
Actually, a better way would be to put a listener on the AttachBody.prototype.bind = function (decorated, container, $container) {
var self = this;
var setupResultsEvents = false;
decorated.call(this, container, $container);
container.on('open', function () {
self._showDropdown();
self._attachPositioningHandler(container);
if (!setupResultsEvents) {
setupResultsEvents = true;
container.on('results:all', function () {
self._positionDropdown();
self._resizeDropdown();
});
container.on('results:append', function () {
self._positionDropdown();
self._resizeDropdown();
});
container.on('results:message', function () { //This listener added as messages clear results and need to reposition the dropdown
self._positionDropdown();
self._resizeDropdown();
});
}
});
... I'll write up a PR for review real quick. |
Resolves: select2#4614 Previous behavior: whenever a message is displayed, results are cleared out, but dropdown is not repositioned, resulting in a floating search box based off of last position. Fixed behavior: when a message is displayed, reposition the dropdown accordingly
One thing i found is that when i removed the minimumInputLength: 1, the issue went away, leaving this here in case it helps anyone, i changed my ajax call to work of a delay rather |
Why this issue is not yet merged ? |
@laudeco , I think at this point the library lacks a dedicated maintainer. Maintaining an open source project can be a lot of work, and I think there's really only been 1 for the last year (who has since dropped off). As a result, Select2 doesn't seem to be getting updates or bug fixes merged in. As an alternative, I've left my fork that I'm using for the PR open because we needed the fix as well. So our project references my fork directly in our |
Continue discussion here -> #5196 |
Have you ever considered changing the position of the dropdown? |
This fixes an issue which was usually observed when working with AJAX results sets and having a message being displayed, usually the minimum characters message or an error. The dropdown would display up, like it was supposed to, but the message would appear to be floating above the container and detached. This was occuring because the dropdown position was not being calculated whenever a message was displayed in the results, only when the results were loaded or new results were appended to an existing results set. There are plenty of situations where this could have caused issues, but somehow most of the reports were around a very specific situation with AJAX which could be reproduced on the examples site. Fixes #4614 Fixes #4616 Fixes #5253 Closes #5196
This fixes an issue which was usually observed when working with AJAX results sets and having a message being displayed, usually the minimum characters message or an error. The dropdown would display up, like it was supposed to, but the message would appear to be floating above the container and detached. This was occuring because the dropdown position was not being calculated whenever a message was displayed in the results, only when the results were loaded or new results were appended to an existing results set. There are plenty of situations where this could have caused issues, but somehow most of the reports were around a very specific situation with AJAX which could be reproduced on the examples site. Fixes #4614 Fixes #4616 Fixes #5253 Closes #5196
Hey guys, I just ran into this problem on a wordpress website I'm working on.. Because I was logged in on the site a When I logged out, and the html top margin was removed, the select dropdown positioned fine. I did a bit of digging and found in // ln 192
var parentOffset = {
top: 0,
left: 0
};
if (
$.contains(document.body, $offsetParent[0]) ||
$offsetParent[0].isConnected
) {
parentOffset = $offsetParent.offset(); // returns `{left: 0, top: 32}` when logged in
} the Hope this helps :) |
I had a similar problem where the top position of the dropdown was messed up specifically on mobile devices. Nonetheless, the solution was simple for this problem. You need to add the
This is needed because if |
@ashwani-pandey take care that if you have multiple selects you'll break things (as I did :) $(".select2").each((_i, e) => {
var $e = $(e);
$e.select2({
dropdownParent: $e.parent()
});
}) Why on earth is this not the default behavior in |
$(document).on('select2:open', () => { |
I had the same problem @bennettstuart did but in case you're using woocommerce they are using a forked polished version of select2 where this issue is taken care of. You can use the woocommerce forked version like this: wp_enqueue_style('select2'); And initialize with .selectWoo() like mentioned here: https://github.com/woocommerce/selectWoo |
Steps to reproduce the issue
Expected behavior and actual behavior
When I follow those steps, I see the search box disconnected from the select control
I was expecting it near the select control
Environment
Browsers
Isolating the problem
The text was updated successfully, but these errors were encountered: