问题描述
我使用猎犬获取一些预先输入的数据。我的猎犬对象:
i use bloodhound to get some data for typeahead. My Bloodhound Object:
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返回所有可用的记录。在我的例子中,它什么都不返回。
This works fine, when my query is "L" like in the example, or another string. But I want bloodhound to return all available Records, when my query is "". In my example, it returns nothing.
我看到,那个猎犬有一个过滤器参数,但我不知道如何使用它。
I saw, that bloodhound has a filter argument, but I don`t know how to use this.
有人可以帮助我吗?
推荐答案
我认为可能有更好的方法可以做到这一点。但它仍然取决于可能改变的内部猎犬实施。
I think there might be a better way of doing this. But it still depends on internal bloodhound implementation that may change.
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
});
此代码利用名为及其功能 all()
返回Bloodhound存储的完整数据列表。
This code takes advantage of implementation of Bloodbound internal search engine called SearchIndex
and its function all()
that returns full list of data stored by Bloodhound.
答案的启发:
- joews answer
这篇关于typeahead.js:返回空查询的所有Bloodhound记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!