4

我尝试了这个解决方案,但我得到了这个错误:

未捕获的 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()
}) ;

我的错误在哪里?谢谢

4

3 回答 3

6

如果您不想使用 Bloodhound,您可以自定义 Typeahead 对象的“highlighter”和“matcher”方法,使它们变得不区分重音。

如果你想让重音不敏感成为 typeahead 的默认行为,你可以包含一个新的 JavaScript 文件,如下所示:

第 1 步 - 创建一个新文件 bootstrap3-typeahead-ci.min.js

// 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>');
}

第 2 步 - 在 bootstrap3-typeahead.min.js 之后添加到您的页面

<script src="bootstrap3-typeahead.min.js"></script>
<script src="bootstrap3-typeahead-ci.min.js"></script>

当然,您必须注意,由于您要替换这两种方法,因此您应该监视在 typeahead 中发布的新功能是否不会反映在您的自定义方法中。但是,我认为这是一个轻量级且快速的解决方案。

PS.:这是我对 Stack Overflow 的第一个贡献,希望你喜欢!

于 2016-02-06T22:10:02.083 回答
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”。

请注意,我更改了函数参数以使其更准确(即字符不正确,应该是字符),并且我还对正则表达式进行了合理化。

于 2014-03-19T00:28:50.783 回答
1

您可以使用 String.prototype.normalize()

https://developer.mozilla.org/en-[US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize][1]

var queryTokenizer = function(input) {
        input = input.normalize("NFD").replace(/[\u0300-\u036f]/g, "")
        return Bloodhound.tokenizers.whitespace(input);
};
于 2020-10-06T12:21:11.727 回答