Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter out option tags from the original select with Select2?

Seems like a pretty simple idea, but I can't figure out how to filter out option tags of the original select from the select2 dropdown.

Basically, this:

<select id="select">
    <option class="car">Car 1</option>
    <option class="plane">Plane 1</option>
</select>

$("#select").select2();

... should create a fake select with only the option tags with the class car.

like image 205
Robin Avatar asked Mar 16 '13 01:03

Robin


People also ask

How do I filter in select?

Select a cell in the header row. The cell must contain the filter drop down icon. Press and hold the Alt key, then press the Down Arrow key on the keyboard to open the filter menu.

How do I clean my Select2?

In order to clear the selection of those values which are selected using a Select2 drop down,we can use the empty() function. The dropdown can be reset using Jquery. $("#select2_example"). empty();

How do I hide Select2 search box?

In order to hide the search box in select2, you can hide the search box by setting "minimumResultsForSearch" to a negative value. $('select'). select2({ minimumResultsForSearch: -1 });

How do I add options in Select2?

New options can be added to a Select2 control programmatically by creating a new Javascript Option object and appending it to the control: var data = { id: 1, text: 'Barn owl' }; var newOption = new Option(data. text, data.id, false, false); $('#mySelect2'). append(newOption).


2 Answers

It appears the OP found the answer at https://github.com/select2/select2/issues/1048.

The answer is to supply a matcher function.

$("#select").select2({
    matcher: function(term, text, option) {
        return option.hasClass('car');
    }
});

jsfiddle

like image 189
John S Avatar answered Sep 22 '22 12:09

John S


Just to keep this question up-to-date.. The proposed answer with the use of supplying the matcher function is outdated. Since Select2 4.0.0 you need to use a wrapper with the matcher:

function matchStart (term, text) {
  if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
    return true;
  }

  return false;
}

$.fn.select2.amd.require(['select2/compat/matcher'], function (oldMatcher) {
  $(".js-example-matcher-start").select2({
    matcher: oldMatcher(matchStart)
  })
});

For ME, this didn't work. I don't know why. But I've found a work-around for this. Apparently, with Select2 you can also supply a format function to the .select2() method for each option or element in the select. For example, when you want to prepend each element with an icon or something like this. Turns out that the element won't be shown in the dropdown if you return null in the format function, like this:

function formatState(state) {
            if (!state.id) { return state.text; }
            if (state.disabled) { return null; }
            var $state = $('<span>'.concat(state.text).concat("</span>"));
            return $state;
        }

$('#myDropdown').select2({
      templateResult: formatState
});

This worked for me immediately. Perhaps it could help any of you with current implementations.

like image 39
FerdieQO Avatar answered Sep 23 '22 12:09

FerdieQO