matcher.html 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <section>
  2. <h1 id="matcher">Customizing how results are matched</h1>
  3. <p>
  4. Unlike other dropdowns on this page, this one matches options only if
  5. the term appears in the beginning of the string as opposed to anywhere:
  6. </p>
  7. <p>
  8. This custom matcher uses a
  9. <a href="options.html#compat-matcher">compatibility module</a> that is
  10. only bundled in the
  11. <a href="index.html#builds-full">full version of Select2</a>. You also
  12. have the option of using a
  13. <a href="options.html#matcher">more complex matcher</a>.
  14. </p>
  15. <div class="s2-example">
  16. <p>
  17. <select class="js-example-matcher-start js-states form-control"></select>
  18. </p>
  19. </div>
  20. {% highlight js linenos %}
  21. function matchStart (term, text) {
  22. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  23. return true;
  24. }
  25. return false;
  26. }
  27. $.fn.select2.amd.require(['select2/compat/matcher'], function (oldMatcher) {
  28. $(".js-example-matcher-start").select2({
  29. matcher: oldMatcher(matchStart)
  30. })
  31. });
  32. {% endhighlight %}
  33. </section>