【发布时间】:2014-10-23 17:41:03
【问题描述】:
我整天都在挣扎。 我的 typeahead 搜索表达式非常适合远程 json 数据。 但是当我尝试使用与预取数据相同的 json 数据时,建议是空的。在点击第一个标志后,我收到预定义的消息“无法找到任何东西...”,结果为空。
这是我的脚本:
function initialTypeaheadTest(){
var mo_selector = $('*[data-moT="mysearch"]');
var engine = new Bloodhound({
limit: 10
,prefetch: {url:'/api/action/getjsontest'}
//,remote: {url:'/api/action/getjsontest'}
,datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name')
,queryTokenizer: Bloodhound.tokenizers.whitespace
});
engine.clearPrefetchCache(); // only for test purposes
engine.initialize();
mo_selector.typeahead(
{
hint: true,
highlight: true,
minLength: 1
}, {
name: 'engine',
displayKey: 'name',
source: engine.ttAdapter(),
templates: {
empty: [
'<div class="empty-message">',
'unable to find anything that match the current query',
'</div>'
].join('\n'),
suggestion: Handlebars.compile([
'<div id="{{value}}"><p style="max-height:100%;"><strong>{{title}}</strong></p>',
'<span style="font-size:12px;">{{year}}</span></div>'
].join(''))
}
});
}
这就是我的 json:
[{
"year":"02 Feb 2013 at 00:40 by anonymous",
"title":"JavaScript HTML DOM Changing HTML Content",
"value":"1",
"tokens":["JavaScript", "HTML", "DOM", "Changing", "HTML", "Content"]
},{
"year":"02 Feb 2013 at 00:42 by anonymous",
"title":"WDR.de",
"value":"2",
"tokens":["WDR.de"]
},{
"year":"02 Feb 2013 at 00:46 by anonymous",
"title":"Nachrichten, aktuelle Schlagzeilen und Videos - n-tv.de",
"value":"3",
"tokens":["Nachrichten", "aktuelle", "Schlagzeilen", "und", "Videos", "n", "tv.de"]
},{
"year":"02 Feb 2013 at 00:55 by anonymous",
"title":"JavaScript RegExp Object",
"value":"5",
"tokens":["JavaScript", "RegExp", "Object"]
},{
"year":"15 Feb 2013 at 23:24 by anonymous",
"title":"DotnetNuke Module Car Listing Module",
"value":"8",
"tokens":["DotnetNuke", "Module", "Car", "Listing", "Module"]
},{
"year":"08 Feb 2014 at 01:08 by advertiser",
"title":"Empfehlung",
"value":"1000",
"tokens":["Empfehlung"]
}]
原始路径:mattopen.com/api/action/getjsontest
对我来说,一切看起来都很好。 json数据格式正确。所有字符串(例如,在标记中)也用双引号引起来。 为什么远程数据有效但预取无效?
有人能指出我正确的方向吗? 谢谢
【问题讨论】:
标签: jquery json bootstrap-typeahead twitter-typeahead