如何实现像模糊搜索这样的崇高文本?
问题描述:
如何在select2上实现类似升华的模糊搜索?
How can i implement a sublime-like fuzzy search on select2?
例如,输入"sta jav sub"将与"Stackoverflow javascript sublime like"相匹配
Example, typing "sta jav sub" would match "Stackoverflow javascript sublime like"
答
select2 allows you to implement your own "matcher" functions (as seen on their docs), using that and some regexp you can do something like:
$("#element").select2({
matcher: function(term, text, opt) {
//We call to uppercase to do a case insensitive match
//We replace every group of whitespace characters with a .+
//matching any number of characters
return text.toUpperCase().match(term.toUpperCase().replace(/\s+/g, '.+'));
}
});
在过滤/搜索列表时,将对每个select2列表元素调用一个matcher函数,您可以使用该函数实现任何类型的自定义搜索.
A matcher function is invoked against every select2 list element when filtering / searching the list, you could implement any kind of custom search using that.