我爲我的問題做了一個解決方案,但可能不是一個漂亮的問題。 Question about accents
我在這個例子中找到了靈感,並製造了兩隻正常化的獵犬。一個與"Cube" : "Kube"
和一個與"Kube" : "Cube"
。然後,我將獲得Kube和Cube的結果,而不僅僅是Cube或Kube(僅使用一種標準化)。
var charMap = {
'a': /[àáâã]/gi,
'c': /[ç]/gi,
'e': /[èéêë]/gi,
'i': /[ïí]/gi,
'o': /[ôó]/gi,
'oe': /[œ]/gi,
'u': /[üú]/gi
};
var normalize = function (str) {
$.each(charMap, function (normalized, regex) {
str = str.replace(regex, normalized);
});
return str;
};
var queryTokenizer = function (q) {
var normalized = normalize(q);
return Bloodhound.tokenizers.whitespace(normalized);
};
var foo_keywords = [
{name: 'foo1', keywords_query: normalize('foo1à, foo1ç, foo1ê'), keywords: 'foo1à, foo1ç, foo1ê', picture: '/img/foo1.png'},
{name: 'foo2', keywords_query: normalize('foo2à, foo2ç, foo2ê'), keywords: 'foo2à, foo2ç, foo2ê', picture: '/img/foo2.png'},
{name: 'foo3', keywords_query: normalize('foo3à, foo3ç, foo3ê'), keywords: 'foo3à, foo3ç, foo3ê', picture: '/img/foo3.png'}
];
var foo = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace(normalize('keywords_query')),
queryTokenizer: queryTokenizer,
local: foo_keywords
});
foo.initialize();
$('.typeahead').typeahead(
{
hint: true,
highlight: true,
minLength: 1,
},
{
name: 'foo',
displayKey: 'name',
source: foo.ttAdapter(),
templates: {
suggestion: function(el){return '<div style=\"display:table;\"><div style=\"display:table-cell;text-align:left;margin:0;width:100px;\"><img src="'+el.picture+'" style=\'width:100px;height:auto;\'/></div><div style=\"display:table-cell;text-align:left;margin:0;padding:10px;vertical-align:top;">'+el.name+'<br><font style=\"font-size:0.6em;color:#7b7b7b;\">'+el.keywords+'</font></div></div>'}
}
}
);
然後只是用ttAdapter()使用這兩個源。
現在我唯一的問題是,結果將有重複的結果不在規範化列表中。 hmmmm – user1865820