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

Cannot unselect by clicking selected when using dataAdapter #6128

Open
atay opened this issue Dec 14, 2021 · 7 comments · Fixed by #6179 · May be fixed by #6241
Open

Cannot unselect by clicking selected when using dataAdapter #6128

atay opened this issue Dec 14, 2021 · 7 comments · Fixed by #6179 · May be fixed by #6241

Comments

@atay
Copy link

atay commented Dec 14, 2021

When using select2 with dataAdapter, the selected option cannot be deselected by clicking it again on list.
If no dataAdapter option is used, it works fine, option can be deselected by clicking it second time.

Here's example with a bug I described, you can select any option and then try to deselect it by clicking it again.
http://jsfiddle.net/o53fLqum/

@kevin-brown
Copy link
Member

kevin-brown commented Dec 17, 2021

I can reproduce the behaviour that you're seeing, I'm trying to trace it down to a bug or something missing in the custom adapter.


I upgraded your jsfiddle to use the latest release (4.1.0-beta) and the issue still exists. If I log the value of each result when it is displayed, I can see a few interesting things:

  1. When the results are selected, the <option> element is properly injected but it isn't being associated with the data option as the element property
  2. As a result of the above issue, the selected property is instead being set on the data object to true

And when you try to unselect it

  1. The previously created <option> still persists (as expected) but is not unselected (unexpected)
  2. The selected property that was set on the data object is set to false
  3. An unselect event is properly triggered but that is also not attached to the <option> but it does have the modified selected property

And if you try to select it again

  1. The selected property that is set on the data object continues to show as false

@kevin-brown
Copy link
Member

Figured out the issue.

The id property on data objects must be a string or it won't be able to match up with the <option> that is created (since <option>.value is always a string). This is why the element property isn't getting set as expected on the data object which leads to pretty much the rest of the issues we were seeing. Setting the id property to a string in the example jsfiddle solves these issues.

https://select2.org/data-sources/formats#automatic-string-casting

I'm still leaving this open since Select2 should handle this case and automatically cast it, but it looks like something isn't consistently doing that which is causing issues.

@stale
Copy link

stale bot commented Mar 2, 2022

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 2, 2022
@jonahgreenthal
Copy link

The project maintainer deliberately chose to leave this open, so it should be left open

@stale stale bot removed the status: stale label Mar 2, 2022
@stale
Copy link

stale bot commented May 1, 2022

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.

@jayaddison
Copy link
Contributor

jayaddison commented Jul 13, 2022

@kevin-brown I was taking a look into this separately, and arrived at the same conclusion that the cache lookups for item data fail, resulting in a missing data.element property to apply effects to.

At least for a small repro case, there's an alternative solution that doesn't rely on id type-casting -- although it could be a bit hacky (particularly the assumption regarding a single option element per container?).

See: 8258567

Edit: update link to unbreak the commit hyperlink -- please note that it originated from a forked repository, though (not this repository)

@stale stale bot removed the status: stale label Jul 13, 2022
@jayaddison
Copy link
Contributor

jayaddison commented Jul 21, 2022

See: 8258567

Sorry, ignore this attempt at a fix. I was testing it again using a hand-written select2 example, and this time it isn't working (the option elements do not exist), so I don't think it's valid.

Edit: update link to unbreak the commit hyperlink -- please note that it originated from a forked repository, though (not this repository)

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