本文介绍了Typeahead并不显示json中的所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以,我有一个工作正常的twitter typeahead.js脚本,但是注意到,有时(带有一些关键字)它不能显示json的所有结果:
So, I have working twitter typeahead.js script, but noticed, that sometimes(with some keywords) it shows not all results from json:
一种情况:此收到的json中3个对象在哪里,但是搜索表单仅返回第一个对象:
One of the case:Where is 3 objects in this received json, but search form returns only first one:
[
{
"query":"David Flanagan - JavaScript",
"id":"7",
"image":"\u003Cimg src=\u0022http:\/\/bks5.books.google.lt\/books\/content?id=4RChxt67lvwC\u0026printsec=frontcover\u0026img=1\u0026zoom=1\u0026edge=curl\u0026source=gbs_api\u0022\u003E"
},
{
"query":"Stoyan Stefanov - JavaScript Patterns",
"id":"10",
"image":"\u003Cimg src=\u0022http:\/\/bks8.books.google.lt\/books\/content?id=ZEmbAgAAQBAJ\u0026printsec=frontcover\u0026img=1\u0026zoom=1\u0026source=gbs_api\u0022\u003E"
},
{
"query":"Douglas Crockford - JavaScript: The Good Parts",
"id":"9",
"image":"\u003Cimg src=\u0022http:\/\/ecx.images-amazon.com\/images\/I\/518QVtPWA7L._SL160_.jpg\u0022\u003E"
}
]
搜索表单结果:
我的预输入脚本:
$( document ).ready(function() {
var books = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/search/auto/?q=%QUERY',
wildcard: '%QUERY',
}
});
$('#books_search').typeahead({
hint: true,
highlight: true,
minLength: 3,
limit: 5
},
{
name: 'book-search',
display: 'query',
source: books,
templates: {
empty: [
'<div class="empty-message">Unable to find any books that match the current query</div>'
]
}
}).on('typeahead:selected', function(e, data) {
$("#search-form").submit();
});
});
任何想法有什么问题吗?
Any ideas what can be wrong?
推荐答案
所以这是typeahead.js错误,这是解决方案:(已提交,但尚未在master中使用):
So it was typeahead.js bug, here is solution: (it's committed, but not in master yet):
https://github.com/twitter/typeahead.js/pull/1200
这篇关于Typeahead并不显示json中的所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!