Select2 Custom Matcher for Non-Adjacent Keywords


Select2 gives you a customizable select box with support for searching, tagging, remote data sets, infinite scrolling, and many other highly used options.

You can download the select2 plugin from the given link below and more details are also there.
https://select2.github.io

One limitation with select2 is that the default plugin does not provide the searching for Non-Adjacent Keywords in drop down or multi select.

One way is using permutation but it causes load when you have large amount of data or the nested data.so, i have made a patch for that.

The following function will search for the non adjacent keywords in the Select2 Dropdown without any permutation which takes a lot of time in case of large amount of data or nested data.

Just put this function in your document.ready before initializing select2.

$(function () { 

 var keywords;

 $.fn.select2.defaults = $.extend($.fn.select2.defaults, {            
    placeholder: 'Select...',            
    matcher: function(term, text, option) {

      if ($.trim(term) === '') {
          return true;
      }             
      keywords = (term).split(" ");

      for (var i = 0; i < keywords.length; i++) {

       if ((text.toUpperCase()).indexOf((keywords[i]).toUpperCase()) == -1)                        
       {                        
           return false;
       }                                         
      }                
      return true;  
    }
 });

 $("#DropdownID").select2();
});
First


');