【发布时间】:2016-03-11 09:58:53
【问题描述】:
我一直在使用 Spring MVC 应用程序并尝试使用 Twitter Typeahead 来显示建议。然而问题是,虽然建议是从服务器正确获取的,但建议框根本没有出现。相同的代码以前可以工作,但现在不能工作。
我的 javascript 代码是:
var skillSuggestions=new Bloodhound({
datumTokenizer:Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer:Bloodhound.tokenizers.whitespace,
remote:{
url:"/tags/get.html/?searchTerm=%QUERY",
filter:function(x){
return $.map(x,function(item){
return{value:item.name};
});
},
wildcard:'%QUERY',
}
});
skillSuggestions.initialize();
$('#skill-name').typeahead({
hint:true,
highlight:true,
minLength:1
},{
name:'value',
displayKey:'value',
source:skillSuggestions.ttAdapter()
})
对于输入“j”,接收到的 json 响应是:
[{"id":"56d546f5535a3c819f080558","name":"Java","category":"Information Technology","subCategory":"Programming/Software","createdDate":1456817909648,"updatedDate":null,"createdBy":"bob","modifiedBy":null},{"id":"56d93f8e535a773c1f8cc846","name":"Javascript","category":"Information Technology","subCategory":"Programming / Software","createdDate":1457078158043,"updatedDate":null,"createdBy":"bob","modifiedBy":null},{"id":"56d93fa2535a773c1f8cc847","name":"JQuery","category":"Information Technology","subCategory":"Programming / Software","createdDate":1457078178030,"updatedDate":null,"createdBy":"bob","modifiedBy":null},{"id":"56d93fb7535a773c1f8cc848","name":"JavaSE","category":"Information Technology","subCategory":"Programming / Software","createdDate":1457078199012,"updatedDate":null,"createdBy":"bob","modifiedBy":null},{"id":"56e226a47b49d4215eaefa9a","name":"javahhhh","category":"hhhhh","subCategory":"jjjjjj","createdDate":1457661604324,"updatedDate":null,"createdBy":null,"modifiedBy":null}]
我想在建议中显示字段name。我错过了什么吗?请帮忙。我查看了here、here、here 等等,但不知道出了什么问题。
【问题讨论】:
-
这是做什么的??
source:skillSuggestions.ttAdapter() -
我认为链接 Typeahead 和 Bloodhound。文档不清楚
-
源是在您键入时向您显示数据的实际点。我想这是前面的 twitter 引导类型。检查它是否是这个twitter.github.io/typeahead.js/examples。如果是这样,那么您必须实现
substringMatcher函数,以便在您键入时返回要显示的数据 -
@Reddy 不完全是。 substringMatcher 旨在用作使用本地预输入数据源的演示。
-
@AbhisekLamsal 你能在你的过滤器函数中加入一个console.log 语句并验证那里发生了什么吗?
filter:function(x){ return $.map(x,function(item){ console.log(item.name); return{value:item.name}; }); }
标签: javascript jquery json typeahead.js