[英]Twitter typeahead.js with remote array
我想使用twtter的提前输入来显示一些输入建议。 这些建议将来自服务器,因此我需要使用远程表单: http : //twitter.github.io/typeahead.js/examples/
但是远程示例都返回json。 由于我将拥有大量数据,并且我想将其存储在Redis中,所以我认为我不会将数据存储在json中,而是存储在值数组中。 节省内存。
我还没有参与Redis的工作,只是尝试返回一个数组。 但是我只是得到了很多未定义的选项可供选择。
任何指示将不胜感激。
我在Rails上。 假设我将从服务器返回render:text => [“” abc“,” sdf“,” erw“,gfg”],我该如何预先输入以基于该响应显示建议?
这是我想适应采用数组而不是json的示例:
var bestPictures = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: '../data/films/post_1960.json',
remote: '../data/films/queries/%QUERY.json'
});
bestPictures.initialize();
$('#remote .typeahead').typeahead(null, {
name: 'best-pictures',
displayKey: 'value',
source: bestPictures.ttAdapter()
});
谢谢。
我会自己回答
<script type="text/javascript">
$(function() {
var xx = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace("user"),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/home/cp?c=%QUERY',
filter: function(list) {
console.log(list)
return $.map(list, function(name) { return { user: name}; });
}
}
});
xx.initialize();
$('#remote .typeahead').typeahead(null, {
name: 'xx',
displayKey: 'user',
source: xx.ttAdapter()
});
})
</script>
<div id="remote" style="text-align:center; margin-top: 200px;">
<input class="typeahead" type="text">
</div>
因此,从控制器返回render json:%w [foo bar]将显示可用选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.