Skip to content

Conversation

AStoker
Copy link

@AStoker AStoker commented Jan 15, 2018

Resolves: #4614
Link to master branch PR (#5186)

  • 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

This pull request includes a

  • Bug fix
  • New feature
  • Translation

The following changes were made

  • Added an event listener on the container for whenever a message is displayed (using the results:message event) we reposition/resize the dropdown (due to messages clearing out results, but not taking into account new dropdown height).

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
@pedrofurtado
Copy link
Contributor

@AStoker add some unit tests. Just to avoid future breaking changes.

@laudeco
Copy link

laudeco commented Sep 18, 2018

@AStoker could you add the expected unit tests please :)

@AStoker
Copy link
Author

AStoker commented Sep 19, 2018

Was on vacation, sorry. I'm playing catch up this week at work, I'll try and add some tests to ensure it works in the future, unless someone beats me to the punch.

@pedrofurtado
Copy link
Contributor

@AStoker Thanks for feedback! After it, the PR is ok for merge 🎉

@MinionWarrior
Copy link

can you try this, hope this help -->select2/select2-bootstrap-theme#41 (comment)

@florianlacreuse
Copy link

Any feedbacks or news about this PR? Can we expect a fix to be released any time soon? Thanks!

@AStoker
Copy link
Author

AStoker commented Nov 5, 2018

Working on adding tests now, then PR will be updated

@AStoker
Copy link
Author

AStoker commented Nov 5, 2018

@pedrofurtado , looks like tests don't run right now. Also looks like a very out of date version of Grunt is being used that causes builds to fail with node 10. Anything on that?

@AStoker
Copy link
Author

AStoker commented Nov 6, 2018

Also, any feedback on this comment? The test is a bit tricky with qunit because in order to test properly, I need select2 to think that it has to position the dropdown above (looks like there aren't tests for that?). That's what I'm working on right now.

@AStoker
Copy link
Author

AStoker commented Nov 6, 2018

@pedrofurtado, if I can get some guidance on how to write a test so that I can test the options box being above the select box, I'd appreciate it. I'm looking at the tests, and nothing covers those cases (that I can see), and the code makes it difficult to test such cases.

AStoker referenced this pull request Nov 19, 2018
This adds a regression test that verifies the problem with positioning
the dropdown when the parent is a statically positioned element that
still has an offset. This could typically be seen if the body element
has an offset, which unfortunately it almost always does because of the
default user stylesheet in browsers. This was not caught during
pre-release testing because all of the test pages reset the margins and
padding on the body element.

This regression test verifies that the offsets that should be set for
the dropdown are calculated correctly. These were surprisingly difficult
to do because of how the offset is calculated using different
positioning techniques.

These tests are for #3970
@stale
Copy link

stale bot commented Mar 13, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the status: stale label Mar 13, 2019
@florianlacreuse
Copy link

IMHO, this PR should not be closed. This PR should be merged in the near future but first, according to the last messages, some tests need to be written.

@AStoker
Copy link
Author

AStoker commented Mar 25, 2019

Status hasn't changed. Need some guidance in the tests, the code isn't easy to test and the patterns to test it I'm having a hard time reproducing to properly test the behavior.

@ShaunMcGuile
Copy link

ShaunMcGuile commented Apr 11, 2019

Has this PR been merged to the main branch?

@florianlacreuse
Copy link

No, not yet.

@ShaunMcGuile
Copy link

Has the PR been merged?

@stale
Copy link

stale bot commented Jun 22, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the status: stale label Jun 22, 2019
@florianlacreuse
Copy link

Thanks little stale bot but this issue should not be closed.

@stale stale bot removed the status: stale label Jun 25, 2019
@marcosg
Copy link

marcosg commented Jul 25, 2019

It seems to be taking a while to get this PR merged. Here is a workaround that seems to work for many people (including me). My implementation below:

    // data-select2 conflicts with library code. data-something-select2 works
    // set the dropdownParent to fix positioning error
    // https://github.com/select2/select2-bootstrap-theme/issues/41#issuecomment-310825598
    $("select[data-use-select2]").each(function() {
        $(this).select2({
            theme: "bootstrap4",
            dropdownParent: $(this).parent()
        });
    });

kevin-brown pushed a commit that referenced this pull request Jul 28, 2019
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
kevin-brown added a commit that referenced this pull request Jul 28, 2019
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
@morrow95
Copy link

Just tested out version 4.0.9 where this is listed as one of the 'fixes'. For me, it is doing the exact opposite. I never had a problem with positioning when opened above with prior versions and now I have a small gap between the actual select box and the dropdown (only when opened above). Typing in the search box creates a huge gap. I found that changing the screen size (like minimizing it) must force a recalculation and it aligns fine afterwards. Also, when typing in the search box the same appears to happen when when no results are found, but while there are results it stays out of position.

For me, 4.0.9 cannot be used with this problem and I will be staying with 4.0.8 until this is figured out properly.

The problem happens with or without 'dropdownParent' set. Not sure if it matters, but I am using 'selectionAdapter' and 'dropdownAdapter', however, there was no issue with these when using 4.0.8.

@kevin-brown
Copy link
Member

@morrow95 there's a nonzero chance that the bug you're describing is unrelated to this fix, especially if it's "corrected" when a message is displayed. Can you make a new ticket for it (and reference this PR comment)?

@morrow95
Copy link

morrow95 commented Aug 26, 2019

@kevin-brown Same everything on my end except I loaded 4.0.9 and have this problem. I load 4.0.8 back and it goes away. Looking at the 'change' list on the release page this seemed like the most logical change that caused this.

@kevin-brown
Copy link
Member

@morrow95 I can kind of reproduce the issue you're probably seeing on the documentation website.

Since this is a pull request, we can't reopen the ticket and look into the issue further. So can you please create a new ticket for this issue and we can triage it there.

@ashwani-pandey
Copy link

ashwani-pandey commented Oct 15, 2020

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, as @marcosg also mentioned above. You need to add the dropdownParent property in your select2 call. Something like what I have mentioned below.

this.$('#country').select2({
  placeholder: 'Please select',
  dropdownParent: this.$('#country').parent(),
})

This is needed because if dropdownParent attribute is not present, it is equivalent to saying
dropdownParent: $(document.body) and therefore you will find your dropdown html attached at the very end of the body.

@FoamyGuy
Copy link

FoamyGuy commented Nov 1, 2022

It's a bummer that there was no guidance on testing available and this PR ended up stalling out.

This fix seems to work perfectly and resolves a pretty glaring issue where the dropdown does not render in the correct location (instead it's way further away from it's select).

Thank you @AStoker for keeping your branch available. I've switched my project over to use your branch and the problem is resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong search box position when dropdown opens above