select2 search - match only words that start with search term

前端 未结 2 1743
被撕碎了的回忆
被撕碎了的回忆 2021-01-11 11:22

I migrated from chosen to select2 plugin because it works better for me, but its documentation is very poor when compared to chosen. Could anyone tell me what option(s) shou

2条回答
  •  一向
    一向 (楼主)
    2021-01-11 12:13

    Select2 provides an example in the documentation on how to use a custom matcher function for matching search terms to search results. The example given is this exact use case.

    function matchStart (term, text) {
      if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
        return true;
      }
     
      return false;
    }
     
    $.fn.select2.amd.require(['select2/compat/matcher'], function (oldMatcher) {
      $("select").select2({
        matcher: oldMatcher(matchStart)
      })
    });
    
    
    
    
    
    

提交回复
热议问题