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 multiple select separator removes other options on unselect #3416

Closed
Henryk-Tunguz opened this issue May 27, 2015 · 3 comments
Closed
Labels

Comments

@Henryk-Tunguz
Copy link

Hello!

I wanted to use the multiple select part of select2 with optgroups and options.I planned on each option having an id, and then each optgroup's id being the list of those id's.

ex:

id: my_id_one, my_id_two, text: my_optgroup_one
  id: my_id_one, text: one
  id: my_id_two, text: two
id: my_two_a, my_two_b, my_two_c, text:my_optgroup_two
  id: my_two_a, text: a
  id: my_two_b, text: b
  id: my_two_c, text: c

This works great! However, when I go ahead and click the little "x" to unselect an optgroup, all the children options are removed from the select list and are unable to be selected.

Another example (given the template I made above):

Select a, Unselect a => Works!
Select my_optgroup_a, check val of select box, val is my_two_a, my_two_b, my_two_c, Unselect my_optgroup_a => a, b, and c are no longer selectable from the list.

This works if I make my own custom separator for the ID's, but then I'd have to parse out and replace the select2 separator with my custom separator.

I imagine in the code of select2, it goes ahead and does a remove on the specified ID. Since, in my case the ID is a list of comma separated ID's, it makes sense that all the specified ID's would be removed from the select list.

Is there a suggestion, or an alternative way I should be looking at to accomplish my goal?

Thank you!

@marseille
Copy link

I forgot to mention -

I am using Select2 v3.5.2 in conjunction with select2-bootstrap-css 1.4.6

@marseille
Copy link

Looking at the code a bit more closely, it would seem related to line #3168 -

 if (indexOf(id, val) >= 0) {
                    choice.addClass("select2-selected");
                    // mark all children of the selected parent as selected
                    choice.find(".select2-result-selectable").addClass("select2-selected");
                }

and also line #3145 -

   while((index = indexOf(this.id(data), val)) >= 0) {
                val.splice(index, 1);
                this.setVal(val);
                if (this.select) this.postprocessResults();
            }

As the id is a string of other id's, and it looks like val is an array of those id's. Asking if, the id's found with select2 classes are contained inside the val array.

@pedrofurtado
Copy link
Contributor

Sorry, the 3.x version is not maintained anymore. But feel free to fork the repo and fixes what you need.

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

No branches or pull requests

4 participants