Skip to content

Instantly share code, notes, and snippets.

@jakecraige
Last active December 21, 2015 12:59
Show Gist options
  • Save jakecraige/6309692 to your computer and use it in GitHub Desktop.
Save jakecraige/6309692 to your computer and use it in GitHub Desktop.
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
=label_tag :school_district, "School District"
= hidden_field_tag :school_district,
'',
:class => 'select2 ajax',
:data => { :source => reports_autocomplete_path('schooldistrict'), :column => 'schooldistrict' }
match '/reports/autocomplete/:column_name' => 'reports#autocomplete', as: :reports_autocomplete
$('.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