jquery select2 plugin how to match only beginning of word

若如初见. 提交于 2019-12-11 11:47:25

问题


I am using select2 in my website and i want the autocomplete to match only the beginning of the word. For example, if I type "CA" I want CAmeroun to appear and not "vatiCAn".


回答1:


I figured out how to resolve this by searching in the documentation (here https://github.com/select2/select2/issues/428).

In select2 library, replace in select2.js :

matcher: function(term, text) {
        return stripDiacritics(''+text).toUpperCase().indexOf(stripDiacritics(''+term).toUpperCase()) >= 0;
    },

by :

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

And tadaaa. It works. I hope someone who is better in JS (99% of JS developers) could give a better answer or create a good patch.

Don't forget to minify your JS ;) !



来源:https://stackoverflow.com/questions/31461303/jquery-select2-plugin-how-to-match-only-beginning-of-word

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!