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

configuring/overwriting minimumResultsForSearch via data attributes #3199

Closed
sn3p opened this issue Mar 26, 2015 · 2 comments
Closed

configuring/overwriting minimumResultsForSearch via data attributes #3199

sn3p opened this issue Mar 26, 2015 · 2 comments

Comments

@sn3p
Copy link

sn3p commented Mar 26, 2015

I'm trying to configure (overwrite) the minimumResultsForSearch option via data-attributes, but this doesn't work:

<select data-minimum-results-for-search="Infinity">

Also tried data-minimumResultsForSearch (camelCase and lowercase).

Setting it via javascript works as aspected:

$('select').select2({ minimumResultsForSearch: Infinity });

JSFiddle

It seem to be working in v4.0.0-rc.2 but I'm still using v3.5.2 (select2-rails isn't updated yet).

Is this a known issue? Is there a workaround?

@kevin-brown
Copy link
Member

data-* attribute support for most options is new in 4.0.0.

miquel-cabanas added a commit to miquel-cabanas/InvoicePlane that referenced this issue Aug 12, 2018
needed: either when there is a short number of options, or options are
hard coded (e.g. yes/no questions). See:

https://stackoverflow.com/questions/17480040/select2-hiding-the-search-box
(in particular, Aaron Hudon's answer)

select2/select2#3199

select2/select2#3189
miquel-cabanas added a commit to miquel-cabanas/InvoicePlane that referenced this issue Aug 12, 2018
needed: either when there is a short number of options, or options are
hard coded (e.g. yes/no questions). See:

https://stackoverflow.com/questions/17480040/select2-hiding-the-search-box
(in particular, Aaron Hudon's answer)

select2/select2#3199

select2/select2#3189
miquel-cabanas added a commit to miquel-cabanas/InvoicePlane that referenced this issue Aug 12, 2018
needed: either when there is a short number of options, or options are
hard coded (e.g. yes/no questions). See:

https://stackoverflow.com/questions/17480040/select2-hiding-the-search-box
(in particular, Aaron Hudon's answer)

select2/select2#3199

select2/select2#3189
@razvanioan
Copy link

If somebody still wants to configure any option using data attribute (even for older versions of select2) you can use the following solution:

  • configure any custom data attribute (but not used by select2 itself) using semicolon separated list of options (key:value), exactly like UIkit v3 framework does for it's components options, something like:
<select data-options="minimumResultsForSearch:Infinity; param: hehe; float : 45.34">...</select>
  • parse the data-options string and build options object (detecting numbers as well)
$("select").each(function (idx, select) {
  var options = $(select).data("options");

  if (options) {
    options = options.split(/\s*;\s*/).reduce(function (a, c) {
      c = c.split(/\s*:\s*/);

      a[c[0]] = isNaN(c[1]) ? c[1] : (parseInt(c[1], 10) == c[1] ? parseInt(c[1], 10) : parseFloat(c[1]));
      
      return a;
    }, {});

    $(select).select2(options);
  } else {
    $(select).select2();
  }
}

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

3 participants