Created
October 15, 2012 22:59
-
-
Save dinnouti/3896201 to your computer and use it in GitHub Desktop.
Typeahead / Autocomplete with Twitter Bootstrap, Rails, simple_form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= f.input :nome, :collection => get_schools_name, :include_blank => false, :input_html => {:rel => 'autocomplete', :data_default => @school.nome} %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ -> | |
$('select[rel="autocomplete"]').each -> | |
option = [] | |
$(this).find('option').each -> | |
option.push $(this).text() | |
input = $('<input>') | |
input.attr('type','text') | |
input.attr('name', $(this).attr('name') ) | |
input.attr('id', $(this).attr('id') ) | |
input.attr('class', $(this).attr('class') ) | |
input.attr('data-provide', 'typeahead' ) | |
input.val($(this).attr('data_default')) | |
$(this).replaceWith(input) | |
$(input).typeahead({ | |
source: option | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_schools_name | |
School.uniq.pluck(:nome) | |
end |
Did a similar thing with :
= f.input :CountryCode,
label: "Country Code",
input_html: { rel: 'autocomplete', data_default: @location[:CountryCode], autocomplete: "off", data: {source: get_country_codes, provide: "typeahead" } }
No JS/Coffee is required, Just a helper method to supply data-source
with an array.
(Forgive the weird field names, the project is hanging off a legacy database.)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried using your solution but "typeahead" isn't working and I have bootstrap 3. can anyone help?