Reputation: 3281
i was wondering if someone could help with me bootstraps "typeahead" with rails and jquery autocomplete
in my 'new' action template, i have...
<div class="tags">
<%= f.label :tag_names, "Tags" %>
<%= f.text_field :tag_names,
data: { autocomplete_source: tags_path} %>
</div>
which then goes to my index action of my tags controller
def index
@tags = Tag.order(:name).where("name like ?", "%#{params[:term]}%")
render json: @tags.map{ |tag| {:label => "#{tag.name} x #{tag.count}", :value => tag.name} }
end
and below is my jquery ui autocomplete code, though i dont think its very necessary here...but here it is incase
$(function() {
var extractLast, split;
split = function(val) {
return val.split(/,\s*/);
};
extractLast = function(term) {
return split(term).pop();
};
return $("#lesson_tag_names").bind("keydown", function(event) {
if (event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) {
return event.preventDefault();
}
}).autocomplete({
source: function(request, response) {
return $.getJSON($("#lesson_tag_names").data("autocomplete-source"), {
term: extractLast(request.term)
}, response);
},
search: function() {
var term;
term = extractLast(this.value);
if (term.length < 1) {
return false;
}
},
focus: function() {
return false;
},
select: function(event, ui) {
var terms;
terms = split(this.value);
terms.pop();
terms.push(ui.item.value);
terms.push("");
this.value = terms.join(", ");
return false;
}
});
});
the autocomplete works, however i can't seem to get it to work with twitters bootstrap. i tried providing
data: { autocomplete_source: tags_path, provide: "typeahead"} %>
and
$('.typeahead').typeahead()
into my application.js file. but it doesn't seem to work. i also tried giving my form an id attribute with...
<%= f.text_field :tag_names, id: "autocomplete",
data: { autocomplete_source: tags_path} %>
so i can do something like
$('#autocomplete').typeahead()
however by giving my form an :id attribute, my autocomplete stops working. so...im not really sure what is wrong. are there other ways of doing it?
help would be much appreciated. thanks
Upvotes: 4
Views: 2527
Reputation: 2718
I had a similar issue and ended up styling the jQuery UI autocomplete like the bootstrap typeahead (basically applying bootstrap CSS to jQuery UI elements).
How I did it: http://criticalthinking.tumblr.com/post/17940560421/making-jquery-ui-look-like-bootstrap
Upvotes: 2