I'm running a ruby on rails app, and I'm using jQuery to show some autocomplete suggestions in a form. The autocomplete suggestions are sourced from 2 different arrays which I merge into one, and then display.
Now, the issue is that I need to display results from one file in a different manner than results from another. How do I do this? That is, they will all be in the same autocomplete box, but the results from one may have a different font, color, etc. (a separate CSS hook?)
autocomplete.js:
$(function() {
$('.autocomplete_field').autocomplete({
source: '/locations/autocomplete.js'
});
});
locations_controller.rb:
def autocomplete
respond_to do |format|
format.js do
client = GooglePlacesAutocomplete::Client.new(:api_key => "mykey")
locations = client.autocomplete(:input => params[:term], :lat => "x", :lng => "y", :radius => "25000")
add = Location.all.map { |l| { :label => l.address, :value => l.address } }
locations = locations.predictions.map { |l| {:label => l.description, :value => l.description} }
final = add + locations
render :json => final
end
end
end
As you can see, the locations_controller simply renders a JSON object which is the two concatenated arrays to the URL /locations/autocomplete.js, which the autocomplete.js renders.
I realize I may need to modify my controller to return 2 separate JSON objects, one for add, and one for location. I can do this, but I do not know how to merge them together in the final autocomplete, and how to format the queries from them differently.
I'm very inexperienced in javascript/jquery, so I'd appreciate an answer which explains those components well, even if you gloss over the required changes to the Rails app.
This is what it boils down to:
When I view the HTML for the autocomplete box, each autocomplete item is as follows:
<li class="ui-menu-item" role="menuitem">
<a class="ui-corner-all" tabindex="-1">(autocomplete suggestion)</a>
</li>
I simply need to add an additional class besides "ui-menu-item" and "ui-corner-all" to the results obtained from one JSON, which will not be present in the results obtained from the other.
Now, I see here that its possible to add an additional class by modifying the .renderItem function, but how do I do apply this to every element in a certain JSON object (js approach), or apply this to every element in a ruby array beforehand (through rails)?