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

how to allow duplicate selections #3341

Closed
epikhighs opened this Issue May 12, 2015 · 4 comments

Comments

Projects
None yet
3 participants
@epikhighs

epikhighs commented May 12, 2015

Is there a way for select2 v3.5.x to allow selecting the same item multiple times?

It seems the default behavior (which is probably the most common use case) is to remove the selected item from the dropdown list. But I'd like to keep the item in the dropdown list and be able to add more duplicates of the same item.

Thanks.

@kevin-brown

This comment has been minimized.

Show comment
Hide comment
@kevin-brown

kevin-brown May 12, 2015

Member

Not in 3.5.x, but it is allowed in 4.0.0.

#1002

Member

kevin-brown commented May 12, 2015

Not in 3.5.x, but it is allowed in 4.0.0.

#1002

@epikhighs

This comment has been minimized.

Show comment
Hide comment
@epikhighs

epikhighs May 12, 2015

@kevin-brown thanks for the response. But allowing duplicate IDs is not necessarily the feature I was looking for.

I was hoping that when clicking on a selection in the drop down, that selection would not disappear, but rather, you could select that option multiple times.

Simply adding duplicate options into the select tag is not the feature I'm inquiring about.

I think with select2 4.x, maybe a plugin could be written to support this? I'm trying to see if this would be a 'simple' change or a pretty involved change.

Thanks.

epikhighs commented May 12, 2015

@kevin-brown thanks for the response. But allowing duplicate IDs is not necessarily the feature I was looking for.

I was hoping that when clicking on a selection in the drop down, that selection would not disappear, but rather, you could select that option multiple times.

Simply adding duplicate options into the select tag is not the feature I'm inquiring about.

I think with select2 4.x, maybe a plugin could be written to support this? I'm trying to see if this would be a 'simple' change or a pretty involved change.

Thanks.

@kevin-brown

This comment has been minimized.

Show comment
Hide comment
@kevin-brown

kevin-brown May 12, 2015

Member

But allowing duplicate IDs is not necessarily the feature I was looking for.

In order to allow for a selection to be made more than once, Select2 needs to allow for the id to be chosen multiple times (or else you would have to make it unique).

Simply adding duplicate options into the select tag is not the feature I'm inquiring about.

If you want to select something multiple times, there is going to have to be multiple options in the select tag. If there wasn't, the server would only know about the first selection, which sorta defeats the purpose of duplicate selections.

I'm trying to see if this would be a 'simple' change or a pretty involved change.

Select2 allows for multiple selections to be made with the same id, which is what you would be looking for here. The trick would be hiding existing selections from the results, or you would inevitably hit duplicates, but that could be as simple as checking data.element.selected in the templateResult option.

You can actually do this without much effort by closing the evt.params.data.element on select2:select and appending it to the <select>, which would add the option back so it can be selected again. You would also have to delete the <option> on select2:unselect (same param) to avoid them stacking up over time.


This appears to be more of a usage question than a bug report or common feature request. As we continue to keep the Select2 issue tracker geared towards these two areas, bug reports and common feature requests, we are redirecting usage questions to other communities such as the mailing list, IRC, or Stack Overflow.

Member

kevin-brown commented May 12, 2015

But allowing duplicate IDs is not necessarily the feature I was looking for.

In order to allow for a selection to be made more than once, Select2 needs to allow for the id to be chosen multiple times (or else you would have to make it unique).

Simply adding duplicate options into the select tag is not the feature I'm inquiring about.

If you want to select something multiple times, there is going to have to be multiple options in the select tag. If there wasn't, the server would only know about the first selection, which sorta defeats the purpose of duplicate selections.

I'm trying to see if this would be a 'simple' change or a pretty involved change.

Select2 allows for multiple selections to be made with the same id, which is what you would be looking for here. The trick would be hiding existing selections from the results, or you would inevitably hit duplicates, but that could be as simple as checking data.element.selected in the templateResult option.

You can actually do this without much effort by closing the evt.params.data.element on select2:select and appending it to the <select>, which would add the option back so it can be selected again. You would also have to delete the <option> on select2:unselect (same param) to avoid them stacking up over time.


This appears to be more of a usage question than a bug report or common feature request. As we continue to keep the Select2 issue tracker geared towards these two areas, bug reports and common feature requests, we are redirecting usage questions to other communities such as the mailing list, IRC, or Stack Overflow.

@alexbispo

This comment has been minimized.

Show comment
Hide comment
@alexbispo

alexbispo Dec 7, 2016

Hi @kevin-brown. I'm having same problems trying to understand it. Can you show me a sample code, because theselect2:select event is not called when option is selected by second time.

ps: my data is loaded by ajax. Thanks.

alexbispo commented Dec 7, 2016

Hi @kevin-brown. I'm having same problems trying to understand it. Can you show me a sample code, because theselect2:select event is not called when option is selected by second time.

ps: my data is loaded by ajax. Thanks.

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