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

For problematic ajax sources, Select2 cannot unselect options, no errors given in the console. #6173

Open
pkExec opened this issue Jul 5, 2022 · 2 comments

Comments

@pkExec
Copy link

pkExec commented Jul 5, 2022

Bug

For problematic ajax sources, Select2 cannot unselect options, no errors given in the console.

Expected

Select2 should be able to unselect options
OR
Select2 should throw an error that an entry is problematic.

Replicate

  1. Go to: https://jsbin.com/qanemejofe/edit?html,js,console,output
  2. Search for something in the first select box.
  3. Select an option. Try to unselect it - Fails.

Causes

  1. An ajax source may contain an item with null ID.
  2. An ajax source may contain an item with a numeric ID.

Workaround

  1. Check each item to make sure it has a valid ID before including it.
  2. Cast the ID to string.

Both options are shown in the second select box in jsbin.

@kevin-brown
Copy link
Member

I don't have the docs open right now but I'm fairly certain these are both documented. The id must be a string and anything else is considered unsupported. There are examples for how to remap and convert keys within Select2 for cases where the API is not right.

@pkExec
Copy link
Author

pkExec commented Jul 6, 2022

I don't have the docs open right now but I'm fairly certain these are both documented. The id must be a string and anything else is considered unsupported. There are examples for how to remap and convert keys within Select2 for cases where the API is not right.

Even if it's documented in the docs (it's not; there are several issues where the "id must be a string" is supposed to be taken care of by the library, see #4227 (comment), #6128 and so on), the point is that one random feature of the library (unselecting) shouldn't break silently. It's very hard to track down why one box suddenly just had a feature stopped working, only to find out that on that day a data source returned a wrong item.

I also don't see why the workaround I'm using in jsbin couldn't be made the default behavior of the library.

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

No branches or pull requests

2 participants