Last active
December 21, 2015 12:59
-
-
Save jakecraige/6309692 to your computer and use it in GitHub Desktop.
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 autocomplete | |
render :json => autocomplete_for_column(params[:column_name]) | |
end | |
def autocomplete_for_column(column_name) | |
#zipcode and mls num doesnt work | |
valid_names = %w(agentlist_fullname officelist_officename officesell_officename schooldistrict) | |
if valid_names.include? column_name | |
Property.select(column_name).where("#{column_name} LIKE ?", "%#{params[:q]}%").uniq | |
else | |
"Error! Column Name Not Allowed!" | |
end | |
end |
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
=label_tag :school_district, "School District" | |
= hidden_field_tag :school_district, | |
'', | |
:class => 'select2 ajax', | |
:data => { :source => reports_autocomplete_path('schooldistrict'), :column => 'schooldistrict' } |
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
match '/reports/autocomplete/:column_name' => 'reports#autocomplete', as: :reports_autocomplete |
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
$('.select2').each(function(i, e) { | |
var select = $(e); | |
options = {}; | |
if(select.hasClass('ajax')) { | |
var source = select.data('source'); | |
var label = select.prev('label').text(); | |
var columnName = select.data('column'); | |
options.id = function (result) { | |
return result[columnName]; | |
}; | |
options.placeholder = "Search for " + label; | |
options.minimumInputLength = 3; | |
options.ajax = { | |
url: source, | |
dataType: 'json', | |
quietMillis: 100, | |
data: function (term, page) { // page is the one-based page number tracked by Select2 | |
return { | |
q: term, //search term | |
page_limit: 10, // page size | |
page: page, // page number | |
}; | |
}, | |
results: function (data, page) { | |
return { results: data }; | |
} | |
} | |
options.formatResult = function(result) { | |
return result[columnName]; | |
}, | |
options.formatSelection = function(result) { | |
return result[columnName]; | |
}, | |
options.dropdownCssClass = "bigdrop"; | |
} else { | |
options.width = "off"; | |
options.placeholder = "Enter some zip codes"; | |
options.tags = ["Type in a zip code followed by a space or comma"]; | |
options.tokenSeparators = [",", " "]; | |
} | |
select.select2(options); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment