如何使匹配器忽略变音符号 - JQUERY

2023-12-22

我正在尝试让 Typeahead 能够处理重音。到目前为止,还没有雪茄。

文件 typeahead.json 包含如下数据:{"0":"João","1":"Rogério","2":"Fábio"}

JQuery 代码如下所示:

$.getJSON('./banco/typeahead.json',function(data){

    var substringMatcher=function(strs){
        return function findMatches(q,cb){
            var matches, substrRegex;
            matches=[];
            substrRegex=new RegExp(q,'i');
            $.each(strs,function(i, str){
                if(substrRegex.test(str)){
                    matches.push({value: str});
                }
            });
            cb(matches);
        };
    };

    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 str;
    }

    var banco=normalize(data);

    $('.typeahead').typeahead({
        hint: true,
        highlight: true,
        minLength: 3,
        //accentMap: charMap,
    },{
        name: 'banco',
        displayKey: 'value',
        source: substringMatcher(banco),
    });

});

在这种情况下,HTML/CSS 是完全无关紧要的:

<input
    class="typeahead tt-query"
    type="text" autocomplete="off" spellcheck="false"
>

感谢您的任何指示。


Try

$(function() {

  $.getJSON('./banco/typeahead.json'
  , function (data) {

    var substringMatcher = function (strs) {
        return function findMatches(q, cb) {
            var matches, substrRegex;
            // an array that will be populated with substring matches
            matches = [];
            // added `á`:`a` to `charMap` ,
            // e.g., try "à" === "á" : false
            var charMap = {
                "á": "a",
                "à": "a",
                "â": "a",
                "á": "a",
                "é": "e",
                "è": "e",
                "ê": "e",
                "ë": "e",
                "ï": "i",
                "î": "i",
                "ô": "o",
                "ö": "o",
                "û": "u",
                "ù": "u"
            };
            // generate string "map" of `data`
            var _map = $.map(data, function (v, k) {
                return [v]
            }).join(",").replace(/,/g, "");
            var temp = [];
            var count = null;
            var regex, r, _temp;
            var normalize = function (str, map) {
                var _temp = new RegExp(str, "i");
                $.each(charMap, function (chars, normalized) {
                    // var regex = new RegExp('[' + chars + ']', 'gi');
                    // str = str.replace(regex, normalized);
                    if (_temp.test(normalized)) {                         
                        ++count;
                        temp.push(chars);
                        var _str = temp.join("|");
                        if (count === temp.length) {
                            _str = new RegExp(_str, "gi");
                            regex = _str;
                        };
                    };
                });
                // test for `str` in `data` 
                // if present, return `str` as regex,
                // else , return `str` mapped to `charMap`
                var r = _temp.test(map) ? _temp : regex;                     
                return r;
            };
            // regex used to determine if a string 
            // contains the substring `q`

            // pass `data` (typeahead.json) to
            // `normalize` to test for occurences
            // of `str` in `data`
            substrRegex = normalize(q, _map);
            // iterate through the pool of strings 
            // and for any string that
            // contains the substring `q`, 
            // add it to the `matches` array
            $.each(strs, function (i, str) {

                if (substrRegex.test(str)) {

                    // the typeahead jQuery plugin expects 
                    // suggestions to a
                    // JavaScript object, refer to typeahead 
                    // docs for more info
                    matches.push({
                        value: str
                    });
                }

            });

            cb(matches);
        };
    };
    var banco = data;
    $('.typeahead').typeahead({
        hint: true,
        highlight: true,
        minLength: 1
    }, {
        name: 'banco',
        displayKey: 'value',
        source: substringMatcher(banco)
    });
    });
});

jsfiddlehttp://jsfiddle.net/guest271314/urqcq20d/ http://jsfiddle.net/guest271314/urqcq20d/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使匹配器忽略变音符号 - JQUERY 的相关文章