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 hidden or 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 hidden or 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 hidden or 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did a similar thing with :
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.)