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

maximumSelectionLength is overridden by other configuration options #3514

Closed
fkranenburg opened this issue Jun 23, 2015 · 11 comments · Fixed by #5581
Closed

maximumSelectionLength is overridden by other configuration options #3514

fkranenburg opened this issue Jun 23, 2015 · 11 comments · Fixed by #5581

Comments

@fkranenburg
Copy link

The property "maximumSelectionLength" does not work when "closeOnSelect" is set to false. I can select more items without any effective limit.

Check out this fiddle where both situations are shown.
http://jsfiddle.net/f9jszen1/

@sparkhee93
Copy link

Is there a way to work around this bug?

@jmiguel-localvox
Copy link

I'm also having this issue.

@Candyffm
Copy link

Here is a workaround (depending on your exact needs). Enable closeOnSelect and let the select2 dropdown reopen automatically each time the selection has changed:
var poiCatSelection = $('#poi_cats');

poiCatSelection
    .select2({
        multiple               : true,
        closeOnSelect          : true,
        maximumSelectionLength : 3})
    .on('select2:selecting', function(){
        setTimeout(function() {
           poiCatSelection.select2('open');
        }, 0);
    });

`

@grunmin
Copy link

grunmin commented Nov 5, 2016

@Candyffm not really a perfect solution, because container would flash and options' position may change

@alexweissman alexweissman changed the title maximumSelectionLength does not work when closeOnSelect is set to false maximumSelectionLength is overridden by other configuration options Sep 25, 2017
@alexweissman
Copy link
Contributor

@mayashavin has identified that this is an issue with a few other configuration options as well. From her issue (#4900):

Some of the following fields will override maximumSelectionLength:

  1. query
  2. tokenSeperators
  3. closeOnSelect (set it to false with cause maximumSelectionLength) not working.

Libraries

  • jQuery version:1.11.1
  • Select2 version: 4.0.3

Browsers: Google Chrome
Operating System: Windows/Mac OS

Investigation: The dataAdapter prototypes get decorated with methods when maximumSelectionLength first, and with tokenSeperators later. Unfortunately, they both have same method name "query", and one will override the other in dataAdapter respectively.

Demo: https://jsfiddle.net/dpnminh/76shLmt2/

@stale
Copy link

stale bot commented Mar 13, 2019

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 13, 2019
@stale stale bot closed this as completed Mar 20, 2019
@reda-redimpact
Copy link

Having same issue fireFox 65

@fkranenburg
Copy link
Author

Yes. This issue is closed but not fixed.

@reda-redimpact
Copy link

reda-redimpact commented Mar 28, 2019

Ok I found a workarround for this with little Js on select2.

We need to know that maximumSelectionLength is only not allowing user to open the dropdown, it doesn't limit the select inside dropdown if the dropdown kept opened with closeOnSelect.

So as a workarround I just added a listener on my select2 and dispatch a close once it attempts the value of limit :

I used this two options on my select 2 :
closeOnSelect : false, maximumSelectionLength: 3

And I added a condition on change event, once three elements selected I just dispatch the close on my select2 :
$('#appbundle_member').on('select2:select', function (e) {
if($(this).select2('data').length >= 3){
$(this).select2("close");
}
});

Check out this fiddle where I did this update :
http://jsfiddle.net/fg0b9z5h/

@JacobLuckhardt
Copy link

I found a solution that worked for me. #5533

@nileio
Copy link

nileio commented Jun 27, 2019

until there is a merge of bug fix as @redamakhchan mentioned , his solution works perfectly. I made small mod to even pick the value automatically in solution here, so all you have to do now is configure as usual & copy paste the code here changing the element id..

The solution code below forces the dropdown to close if the user reaches the configured maximumSelectionLength

 $("#select2-element").on("select2:select", function(e) {
        if (
          $(this).select2("data").length >=
          $(this).data("select2").results.data.maximumSelectionLength
        ) {
          $(this).select2("close");
        }
      });

@kevin-brown kevin-brown added this to the 4.0.9 milestone Jul 21, 2019
kevin-brown added a commit that referenced this issue Jul 21, 2019
There was a bug where the `maximumSelectionLength` option would not
kick in if the `closeOnSelect` option was enabled. Normally, this
was enabled by someone in their global configuration, but it could
also be seen when somoene selected an option while holding the
meta/ctrl/alt keys. This would implicitly enable the `closeOnSelect`
behaviour, even when it was not globally enabled, and cause the bug.

This fixes that issue by listening to the `select` event which is
triggered whenever an option is selected, and triggers the "maximum
selected" message based on that event. This should now force the
message to be displayed, even when the results did not have to be
queried another time.

Fixes #3514
Fixes #3860
Closes #5333
kevin-brown added a commit that referenced this issue Jul 21, 2019
* Rewrote maximumSelectionLength tests to use container

These brings the tests in line with other tests which we have, and
makes it easier to understand what is actually going on in the tests.

This also removes a redundant set of tests where we were testing with
=> 2 options being allowed. There are no current edge cases that would
have required this.

* Fix maximumSelectionLength being ignored by closeOnSelect

There was a bug where the `maximumSelectionLength` option would not
kick in if the `closeOnSelect` option was enabled. Normally, this
was enabled by someone in their global configuration, but it could
also be seen when somoene selected an option while holding the
meta/ctrl/alt keys. This would implicitly enable the `closeOnSelect`
behaviour, even when it was not globally enabled, and cause the bug.

This fixes that issue by listening to the `select` event which is
triggered whenever an option is selected, and triggers the "maximum
selected" message based on that event. This should now force the
message to be displayed, even when the results did not have to be
queried another time.

Fixes #3514
Fixes #3860
Closes #5333
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants