Skip to content
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

Select2 triggers unselect event but does not unselect the option #4845

Closed
10 of 16 tasks
sgmckay opened this issue Mar 21, 2017 · 4 comments
Closed
10 of 16 tasks

Select2 triggers unselect event but does not unselect the option #4845

sgmckay opened this issue Mar 21, 2017 · 4 comments

Comments

@sgmckay
Copy link

sgmckay commented Mar 21, 2017

Prerequisites

  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate
  • The issue still exists against the latest master branch of Select2
  • This is not a usage question (Those should be directed to the community)
  • I have attempted to find the simplest possible steps to reproduce the issue
  • I have included a failing test as a pull request (Optional)

Steps to reproduce the issue

See this js fiddle for an example of the issue and simpler steps to reproduce
https://jsfiddle.net/xvL14mzv/

  1. Create a select2 that loads data from a ajax datasource and in a process results function return back a js object with id and text properties that are different.
  2. In your select find an option to select and select it
  3. Find that option again in the select. It should be shown as selected and use your mouse to click that option again. This will trigger an unselect event which is expected but the option will not be unselected.

Expected behavior and actual behavior

See
https://jsfiddle.net/xvL14mzv/

When I follow those steps, I see...
Option is not removed from the select but an unselect event is triggered

I was expecting...
Option to be removed from the select. This happens when the id and text properties are the same

Environment

Browsers

  • Google Chrome
  • Mozilla Firefox
  • Internet Explorer

Operating System

  • Windows
  • Mac OS X
  • Linux
  • Mobile

Libraries

  • jQuery version: Edge
  • Select2 version: 4.0.3

Isolating the problem

@alexweissman
Copy link
Contributor

This does not seem to be the same issue as described in #5049, and is not fixed as of 4.0.6-rc.1.

@bgarrison25
Copy link

So this was confirmed as a bug over three months ago. Almost never will you have your id and your text be the same thing yet this only works with an ajax data source if this is the case, making it unusable with the ajax option. Is there a fix incoming "soonish" for this?

@bgarrison25
Copy link

FYI I banged my head on this for an entire day and came across a little piece of your documentation that talks about how values are converted from the id. It states that the value needs to be a string so its best to send in string id's. Taking this into mind I typecast my id to a string in my API and this issue went away. For those of you that have access / control of their API's this is a fix. For the rest of you maybe try mapping the data element so it does better typecasting of id (not my forte). With 400+ issues and counting I don't see a fix coming any time soon.

Also, I tried to look into the code to find out WHY me sending in strings vs ints would be such a big deal since int's usually convert to string rather well and I couldn't find anything. If someone else is more familiar with the code you may be able to use this as a starting point at least.

jbrowning added a commit to jbrowning/select2 that referenced this issue Oct 15, 2018
jbrowning added a commit to jbrowning/select2 that referenced this issue Oct 15, 2018
jbrowning added a commit to jbrowning/select2 that referenced this issue Oct 15, 2018
@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
@stale stale bot closed this as completed Mar 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants