【问题标题】:Typeahead insensitive accent预输入不敏感的口音
【发布时间】:2014-04-23 00:09:55
【问题描述】:

我尝试this solution但我收到了这个错误:

未捕获的rigentError:归一化未定义

这是我的代码:

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()
}) ;

我的错误在哪里?谢谢

【问题讨论】:

  • 变量normalized 未在normalize 函数的范围内定义。我相信你的意思是在这个函数中返回str 变量。

标签: javascript typeahead.js twitter-typeahead bloodhound


【解决方案1】:

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

如果您想让重音不敏感成为预先输入的默认行为,您可以包含一个新的 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 的第一个贡献,希望您喜欢!

【讨论】:

    【解决方案2】:

    更改您的 normalize 函数,使其返回标准化字符串,即

    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”。

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

    【讨论】:

      【解决方案3】:

      你可以使用 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);
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-07
        • 2011-05-14
        • 2019-02-15
        • 2021-03-09
        • 2013-01-21
        • 1970-01-01
        相关资源
        最近更新 更多