我使用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