我想将typeahead.js与json文件一起使用。但是当我输入预先输入时,不会发送ajax请求。
的javascript:
var users = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit: 10,
prefetch: {
url: '/users.json',
filter: function(list) {
return $.map(list, function(user) { return { name: user }; });
}
}
});
users.initialize();
$('#prefetch .typeahead').typeahead(null, {
name: 'users',
displayKey: 'name',
// `ttAdapter` wraps the suggestion engine in an adapter that
// is compatible with the typeahead jQuery plugin
source: users.ttAdapter()
});
rails controller:
def index
@users = User.all
@search = User.select :name
render json: @search
end
答案 0 :(得分:0)
这是使用的正确代码: 在 javascript :
中var users = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit: 10,
remote: {
url: '/users.json',
filter: function(list) {
return $.map(list, function(user) { return { name: user }; });
}
}
});