我不知道为什么不能用遥控器。当本地加载相同的结果/json数组时,它可以正常工作。
我的代码
var bh_engine = new Bloodhound({
datumTokenizer: function(d) {
var my_string = d.company_name;
my_string = my_string.replace(/\s/g, "");
var string_length = my_string.length;
var my_matches = [];
for (var i = 0; i < string_length; i++) {
my_matches.push(my_string.substring(i, string_length));
}
return my_matches;
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: local_source,
prefetch: prefetch_source,
remote: remote_source
});
bh_engine.initialize();
$(apply_on_element + ' .bootstrap-tags-input input#mytaginput').typeahead({
hint: true,
highlight: true,
minLength: 1
}, {
async:true,
displayKey: 'company_name',
source: bh_engine.ttAdapter()
});Json数据
[{"company_name":"Google","code":1},{"company_name":"Facebook","code":2}]用相同的数据进行本地和远程测试。它只会给远程访问带来问题。
有什么想法吗?为什么?
您也可以在这里查看JSfiddle
首先输入"string1“或"string2”等字符串.
然后使用远程数据类型"data30“等等.
它每次都显示json对象的第一个元素。
如您所见,粗体/突出显示功能运行良好,但不提示
当我输入"data50“或至少在列表中第一位时,我只想显示"data50”
发布于 2017-09-27 08:29:48
您可以在远程对象上使用“血犬变换”方法来模拟为本地数据实现的相同行为。
window.bh = bloodhoundSuggestions = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
sufficient: 3,
local: suggestions,
remote: {
url: 'https://www.mockaroo.com/api/generate.json?key=9c13bbb0&schema=typeahead',
transform: function(response) {
// input selector
var input = $("#tagsInput").val();
return response.filter( function (item) {
return input.toLowerCase() == item.value.toLowerCase().substr(0, input.length);
});
}
},
});JsFiddle -只需输入数据
https://stackoverflow.com/questions/46440971
复制相似问题