我使用Bloodhound从数据库中获取数据,然后twitter预先输入以显示搜索框下方的选项。
目前,猎犬部分正在寻找所需的物体,但是先行者没有显示它们。
var artist_retriever = new Bloodhound({
// turns input query into string of tokens to send to database.
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
// URL to fetch information from
url: "/artists?query=%QUERY",
wildcard: '%QUERY',
// Manipulate the array of artists returned, for display to user.
transform: function(array_of_artists){
// array of artists is returned from DB.
// Put each artist into a readable string
array_of_artists = create_artist_descriptions(array_of_artists)
console.log(array_of_artists)
// Returns correctly:
// [
// { artist: "Joe" },
// { artist: "Bob" },
// { artist: "Smith" },
// { artist: "Tom" },
// ]
return array_of_artists
}
},
// turns return value into a string of results, with this 'key' before each result.
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('artist'),
});
// display:
// instantiate the typeahead UI
// https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md
searcher = $('.typeahead').typeahead(
// options:
{
hint: false
},
// datasets:
{
// Where to get data: User the bloodhound suggestion engine:
source: artist_retriever.ttAdapter(),
// Which attribute of each result from the database should be shown:
displayKey: 'artist',
templates: {
notFound: new_artist_option_template(),
footer: new_artist_option_template()
}
}
)
更新
事实证明,在typeahead中有一个奇怪的错误。它似乎只适用于"限制"属性设置为最大值4.如果设置"限制"从5开始,typeahead就什么都不给你了。
searcher = $('.typeahead').typeahead(
// options:
{
hint: false
},
// datasets:
{
// Where to get data: User the bloodhound suggestion engine:
source: artist_retriever.ttAdapter(),
// Which attribute of each result from the database should be shown:
displayKey: 'signature',
limit: 4, // This can do a max of 4! Odd.
templates: {
notFound: new_artist_option_template(),
footer: new_artist_option_template()
}
}
答案 0 :(得分:2)
这个问题已经解决了。请直接查看更新2 。
我已在此JSFIDDLE中重现了此问题。
正如你所说,这是一个错误。你还报告说,如果你做limit:4
,这个bug就会消失。
实际上,在我的最后,或者在FIDDLE中,我遇到过number of results returned = value in limit
时出现此问题。
要在FIDDLE中测试此问题,请执行以下操作: 注意:搜索1947只返回5行。
当限制设置为4时 搜索1947年将返回4个结果。
当限制设置为5时 搜索1947年什么也没有。
限制设为6时 搜索1947返回一个结果 - 第一个结果。
因此,如果您将限制设置为比实际返回的结果数少1,那么这将继续有效。
我还在他们的github page中提交了此问题。我将跟踪此问题,并将根据需要不断更新此答案。
找到similar question on SO here。 “LucianoGarcíaBes”似乎已经找到了解决方案。请指导那里的所有赞成。
基本上他说:
它会在追加它们之前计算渲染提示的数量,所以 如果提示数等于限制,它将附加一个空数组。
为了防止这种情况,我只是改变了第1723和1724行,所以它看起来像这样:
that._append(query, suggestions.slice(0, that.limit - rendered));
rendered += suggestions.length;