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

closeOnSelect = false does not work when minimumInputLength > 0 #2264

Closed
shiznit013 opened this issue Apr 9, 2014 · 23 comments
Closed

closeOnSelect = false does not work when minimumInputLength > 0 #2264

shiznit013 opened this issue Apr 9, 2014 · 23 comments

Comments

@shiznit013
Copy link

The dropdown will close on select. I have created a jsfiddle to demonstrate the behavior.

This worked in 3.4.5. I believe (although have not confirmed), that this may have been broken by pull request https://github.com/ivaynberg/select2/pull/1940

@mkurz
Copy link
Contributor

mkurz commented Apr 15, 2014

Works for me (Chrome, Firefox). Which browser are you using?

@shiznit013
Copy link
Author

This does not work for me in Chrome, Firefox, or IE, but perhaps my understanding of how it is supposed to work is incorrect.

When I type 'O', the dropdown appears with all results matching 'O'. When I select an item, I expect the dropdown to remain open since closeOnSelect = false. What happens is, the filter text, 'O', is cleared, and the dropdown closes. This is frustrating to my users, since they want to select multiple items that match the same filter. They need to re-enter the filter text, select the next item, repeat; which can be quite time consuming. For me it is frustrating, because each filter means a request to the database (not seen in the fiddle). So, I need to have at least 1 database call for every item selected.

@kevin-brown
Copy link
Member

I am able to reproduce this, though I'm not entirely sure where the dropdown gets closed.

@dsueltenfuss
Copy link

It appears to be closing because the entered text is getting cleared at line 2890, regardless of if closeOnSelect is set to false

@seiyria
Copy link

seiyria commented Apr 25, 2014

I also encountered this bug today in the latest Chrome.

@ghost
Copy link

ghost commented Jun 20, 2014

me too..when I have minimumInputLength set

@Cheruman
Copy link

Cheruman commented Jul 7, 2014

I'm having the same issue in both the latest Chrome and IE8: https://github.com/ivaynberg/select2/issues/2512

@khaled-su
Copy link

I have the same problem

zacharymarshal added a commit to zacharymarshal/select2 that referenced this issue Aug 12, 2014
…gth is > 1, multiple is true, and closeOnSelect is false
@gildonei
Copy link

gildonei commented Sep 2, 2014

This isn't working for me too. I'm using the latest chrome version.

@Demak
Copy link

Demak commented Oct 30, 2014

Hello, I am experiencing this issue too, it's especially a problem with an ajax-retrieved results. I have found that this is caused by a onSelect method of MultiSelect2 class, specifically by
line 2984: this.clearSearch();
After result have been cleared, select closes because there is not enough characters in input. Making a check for options before calling it or just commenting it out seem to resolve issue and do not break anything.
Select2 Version: 3.5.1

@Zoomink
Copy link

Zoomink commented Nov 14, 2014

Any idea if this will be solved anytime soon? What can we do meanwhile?

@snichini
Copy link

I've fixed this by replacing

this : (lines 3003 to 3011)

this.clearSearch();
this.updateResults();
if (this.select || !this.opts.closeOnSelect) this.postprocessResults(data, false, this.opts.closeOnSelect===true);

        if (this.opts.closeOnSelect) {
            this.close();
            this.search.width(10);
        } else {
by this :
this.updateResults();
if (this.select || !this.opts.closeOnSelect) this.postprocessResults(data, false,   this.opts.closeOnSelect===true);

if (this.opts.closeOnSelect) {
            this.clearSearch();
            this.close();
            this.search.width(10);
 } else {

I've just moved the "clearSearch();" when closeOnSelect is set to true.
It worked fine to me.
Because the clearSearch(); flushes the search in the

    and the this.countSelectableResults() is always 0 because the ul is flushed above.

    Hope it helps anyone. I'm not sure if this breaks something else so I don't have make a pull request.

@zormandi
Copy link

This is a blocker for one of our clients. Any ideas on when an official fix will be available?

@inieves
Copy link

inieves commented Dec 2, 2014

Hi. I can confirm the above fix by snichini worked for me... i had the same problem.. i can now have the search term remain even after selecting some options... with the minimum number of search characters set to 2.

@Cheruman
Copy link

Cheruman commented Dec 2, 2014

I can also confirm snichini's fix worked for me.

@AtomStar
Copy link

This does not work in v4.0 beta2. @snichini fix is for v3.5. I did the following hack on lines 3384-3388 in v4, which seems to work:

       Dropdown.prototype._onSelect = function () {
            if (this.options.options.closeOnSelect == false)
                return;
            this.trigger('close');
        };

@kevin-brown
Copy link
Member

@AtomStar If you want to roll that up into a decorator, it'd save me the time of adding it back myself. It shouldn't take too long and you can use #2914 as an example for adding in the option.

I was planning on adding it back for 4.0, but haven't had the time.

@amamf
Copy link

amamf commented Mar 12, 2015

The above fix provided by snichini doesn't work for me (3.5.2). It works for the first item only, when I select the second item the search results get closed. I came up with a different solution that works for me. The idea is not to update search results if they shouldn't be closed on select:

           // keep track of the search's value before it gets cleared
            this.nextSearchTerm = this.opts.nextSearchTerm(data, this.search.val());

            this.clearSearch();

            if (this.select || !this.opts.closeOnSelect) this.postprocessResults(data, false, this.opts.closeOnSelect===true);

            if (this.opts.closeOnSelect) {
                this.updateResults();
                this.close();
                this.search.width(10);
            } else { ...

@AlexRocks
Copy link

If you do not want to hack the select2 3.5 code then following work ('select2-selecting' event launch before close event so we can manually close/open):

        $("#category_id").on("select2-selecting", function(e) {
            e.preventDefault();
                uploadSubCats(e.val).then(function(res) {
                    if (res.status == "ok") {
                        if (res.data === 0) {
                            $('#category_id').select2("val", val);
                            $('#category_id').select2("close");                                
                        } else {
                            $('#category_id').select2({"data": res.data});
                            $('#category_id').select2("open");
                        }
                    }
                });                                   

@joergrech
Copy link

@AlexRocks Can you clarify what "uploadSubCats" is doing? Your code itself is not working as this method is missing.

@spycom
Copy link

spycom commented Apr 21, 2016

@AlexRocks Thanks a lot, your code helped me to finish this fix

$("#object-select").on("select2-selecting", function(e) {
                e.preventDefault();
                var target = $(this);
                handleSelection(e, target);
});
function handleSelection(e, target){

                e.preventDefault();

                var data = target.select2('data');
                data.push({
                            'id': e.object.id,
                            'text': e.object.text
                        });

                target.select2('data', data);
                target.select2("open");
            } 

@burakcakirel
Copy link

Thank you @spycom
Our legacy code is still using select2 v3.x so your piece of code saved us. Also, I want to add one line to your fix.
After that:
target.select2("open");
Put this line:
target.select2("positionDropdown");
With doing this, the result dropdown's position will adjust as the input's position

@pedrofurtado
Copy link
Contributor

The issue is related to 3.x version, but the version 3.x is not maintained anymore. But if this bug occurs in 4.x version, please reopen the issue and send us a JSFiddle example code or a PR.

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

No branches or pull requests