我使用 bloodhound 为 typeahead 获取一些数据。我的猎犬对象:
var lastAdresses = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: {
url: '/_dev_data_sources/last_adresses_json.html',
},
limit: 20
});
lastAdresses.initialize().done(function () {
var query = "L";
lastAdresses.get(query, function(suggestions) {
console.log(suggestions);
});
});
当我的查询是示例中的“L”或另一个字符串时,这很好用。但是当我的查询是“”时,我希望猎犬返回所有可用的记录。在我的示例中,它不返回任何内容。
我看到,bloodhound 有一个过滤器参数,但我不知道如何使用它。
有人可以帮帮我吗?
最佳答案
我认为可能有更好的方法来做到这一点。但这仍然取决于可能会发生变化的内部猎犬实现。
var searchEngine = new Bloodhound({...});
function searchWithDefaults(q, sync) {
if (q === '') {
sync(searchEngine.index.all());
} else {
searchEngine.search(q, sync);
}
}
$("#typeahead").typeahead({
minLength : 0,
}, {
name : 'typeahead',
source : searchWithDefaults
});
此代码利用名为 SearchIndex
的 Bloodbound 内部搜索引擎的实现。及其返回 Bloodhound 存储的完整数据列表的函数 all()
。
答案灵感来自:
关于javascript - typeahead.js:在空查询时返回所有 Bloodhound 记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23429918/