Javascript 推特输入前方,猎犬过滤器开始
我想用“startswith”过滤器过滤结果。现在,下面的代码将获取与结果中任何单独单词匹配的所有内容。因此,当用户类型“ex”时,“example”和“one-two-example”都会被过滤掉。如何更改此行为,以便只过滤出“示例”Javascript 推特输入前方,猎犬过滤器开始,javascript,twitter-bootstrap,twitter,typeahead,bloodhound,Javascript,Twitter Bootstrap,Twitter,Typeahead,Bloodhound,我想用“startswith”过滤器过滤结果。现在,下面的代码将获取与结果中任何单独单词匹配的所有内容。因此,当用户类型“ex”时,“example”和“one-two-example”都会被过滤掉。如何更改此行为,以便只过滤出“示例” var repos; repos = new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), queryTokenizer: Bloodhound.toke
var repos;
repos = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit: 10,
prefetch: {
name: 'terms',
url: 'search.php',
}
});
repos.initialize();
$('input.typeahead').typeahead(null, {
name: 'repos',
source: repos.ttAdapter(),
templates: {
empty: '<div class="empty-message">No matches.</div>',
suggestion: Handlebars.compile([
'<div class="term-box" id="{{id}}">',
'<p class="term">{{termname}}</p>',
'</div>'
].join(''))
}
});
var回购;
repos=新猎犬({
datumTokenizer:Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer:猎犬,标记,空白,
限额:10,
预取:{
名称:'条款',
url:'search.php',
}
});
repos.initialize();
$('input.typeahead').typeahead(null{
名称:“回购协议”,
来源:repos.ttAdapter(),
模板:{
空:“没有匹配项。”,
建议:车把([
'',
“{{termname}
”,
''
].加入(“”)
}
});
只需更改子字符串函数,如下所示:
var substringMatcher = function (strs) {
return function findMatches(q, cb) { // an array that will be populated with substring matches
var matches = [];
// regex used to determine if a string contains the substring `q`
//var substrRegex = new RegExp(q, 'i');
// we use starts with instead of substring
var substrRegex = new RegExp('^' + q, 'i');
// 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)) {
matches.push(str);
}
});
cb(matches);
};
};