【发布时间】:2014-02-25 12:23:17
【问题描述】:
我正在使用最后一个 twitter typeahead。
var suggestions = new Bloodhound({
datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.value); },
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {url:'/xxx?text=%QUERY',
ajax:{
type : 'post',
dataType : 'json'
}
}
});
suggestions.initialize();
$('.typeahead').typeahead(null, {
displayKey: 'id',
source: suggestions.ttAdapter(),
templates: {
empty: '<p style="width:50%;margin:auto;">No result</p>',
suggestion: function(object){
var str = ['<p>',
object.id,
'</p><a class="btn btn-info btn-small" href="item.html?id="',
object.id,
'>Open</a>'].join('');
return str;
}
}
});
问题是我收到了 5 个类型的对象
{
id: 0,
field: '',
value: ''
}
所有 5 个对象都有相同的 id,因此是一个结果的字段,应该放入 一个建议。 Typeahead 将它们视为单独的建议,我不想要。
我如何映射 ajax 调用的结果,对其进行转换并将结果传回 typeahead?
【问题讨论】:
标签: javascript typeahead.js typeahead twitter-typeahead bloodhound