我试过了this solution,但我收到了这个错误:
未捕获的ReferenceError:未定义规范化
这是我的代码:
var charMap = {
"à": "a", "â": "a", "é": "e", "è": "e", "ê": "e", "ë": "e",
"ï": "i", "î": "i", "ô": "o", "ö": "o", "û": "u", "ù": "u"
};
var normalize = function(str) {
$.each(charMap, function(chars, normalized) {
var regex = new RegExp('[' + chars + ']', 'gi');
str = str.replace(regex, normalized);
});
return normalized;
}
var queryTokenizer = function(q) {
var normalized = normalize(q);
return Bloodhound.tokenizers.whitespace(normalized);
};
var spectacles = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: queryTokenizer,
prefetch:'spectacles.json',
limit:10,
});
spectacles.initialize();
$('#search').typeahead({
minLength: 1,
hint:false,
highlight: true
},
{
name: 'spectacles',
displayKey: 'value',
source: spectacles.ttAdapter()
}) ;
我的错误在哪里?感谢
答案 0 :(得分:5)
如果您不想使用Bloodhound,您可以自定义'荧光笔'和' matcher'来自Typeahead对象的方法,因此它们变得不重视。
如果你想使重音不敏感的是typeahead的默认行为,你可以包含一个新的JavaScript文件,如下所示:
// function for making a string accent insensitive
$.fn.typeahead.Constructor.prototype.normalize = function (str) {
// escape chars
var normalized = str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
// map equivalent chars
normalized = normalized.replace(/[aãáàâ]/gi, '[aãáàâ]');
normalized = normalized.replace(/[eẽéèê]/gi, '[eẽéèê]');
normalized = normalized.replace(/[iĩíìî]/gi, '[iĩíìî]');
normalized = normalized.replace(/[oõóòô]/gi, '[oõóòô]');
normalized = normalized.replace(/[uũúùû]/gi, '[uũúùû]');
normalized = normalized.replace(/[cç]/gi, '[cç]');
// convert string to a regular expression
// with case insensitive mode
normalized = new RegExp(normalized, 'gi');
// return regular expresion
return normalized;
}
// change 'matcher' method so it became accent insensitive
$.fn.typeahead.Constructor.prototype.matcher = function (item) {
// get item to be evaluated
var source = this.displayText(item);
// make search value case insensitive
var normalized = this.normalize(this.query);
// search for normalized value
return source.match(normalized);
}
// change 'highlighter' method so it became accent insensitive
$.fn.typeahead.Constructor.prototype.highlighter = function (item) {
// get html output
var source = this.displayText(item);
// make search value case insensitive
var normalized = this.normalize(this.query);
// highlight normalized value in bold
return source.replace(normalized, '<strong>$&</strong>');
}
<script src="bootstrap3-typeahead.min.js"></script>
<script src="bootstrap3-typeahead-ci.min.js"></script>
当然,您必须意识到,由于您要更换这两种方法,因此您应该监控typeahead中发布的新功能是否会反映在您的自定义方法中。但是,我认为这是一个轻量级的快速解决方案。
PS:这是我对Stack Overflow的第一个贡献,希望你喜欢它!
答案 1 :(得分:2)
更改规范化函数,使其返回规范化字符串,即
var normalize = function (input) {
$.each(charMap, function (unnormalizedChar, normalizedChar) {
var regex = new RegExp(unnormalizedChar, 'gi');
input = input.replace(regex, normalizedChar);
});
return input;
}
看到我写的这个小提琴,看它有效:
http://jsfiddle.net/Fresh/SL36H/
您可以在浏览器调试控制台中看到规范化的字符串。在我的例子中,“àààèèèùùù”被转换为“aaaeeeuuu”。
请注意,我已经更改了函数参数以使它们更准确(即字符不正确,它应该是char)并且我还合理化了正则表达式。
答案 2 :(得分:0)
您可以使用String.prototype.normalize()
var queryTokenizer = function(input) {
input = input.normalize("NFD").replace(/[\u0300-\u036f]/g, "")
return Bloodhound.tokenizers.whitespace(input);
};