我使用Bloodhound从数据库中获取数据,然后twitter键入以显示搜索框下方的选项。Twitter typeahead只显示血猎犬返回的一些项目
目前,猎犬部分正在查找所需的物体,但前部并未显示它们。
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()
}
}
)
更新
事实证明,有一个在预输入一个奇怪的错误。它似乎只能与“limit”属性一起使用,最多设置为4.如果将“limit”设置为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()
}
}
你解决了吗?目前正在研究类似的东西,并且发现预先学习是一种学习曲线。 –
我刚刚遇到了同样的问题,并将结果限制为4次。谢谢,顺便说一句。然而,你是否提前键入了这个错误,或者是否有一些我们可以跟踪的错误参考号。最后,如果你有时间,而且你确实找到了这个问题的原因,你能否在评论中提到我,以便我得到有关此事的通知。谢谢。 – Rash