Skip to content

Commit

Permalink
* Add option to send more form fields in the query.
Browse files Browse the repository at this point in the history
* Supersedes #159 Add option to send additional fields with the auto complete query. feature
  • Loading branch information
donv committed Jan 8, 2014
1 parent 25ecf54 commit 5e20cd0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,30 @@ If you need to use the id of the selected object, you can use the *id_element* a

This will update the field with id *#some_element with the id of the selected object. The value for this option can be any jQuery selector.

### Sending extra search fields

If you want to send extra fields from your form to the search action,
you can use the **:fields** options which generates a **data-autocomplete-fields**
HTML attribute.

The :fields option accepts a hash where the keys represent the Ajax request
parameter name and the values represent the jQuery selectors to retrieve the
form elements to get the values:

f.autocomplete_field :product_name, '/products/autocomplete_product_name', :fields => {:brand_id => '#brand_element', :country => '#country_element'}

class ProductsController < Admin::BaseController
def autocomplete_product_name
term = params[:term]
brand_id = params[:brand_id]
country = params[:country]
products = Product.where('brand = ? AND country = ? AND name LIKE ?', brand_id, country, "%term%").order(:name).all
render :json => products.map { |product| {:id => product.id, :label => product.name, :value => product.name} }
end
end

The previous example would fetch the extra attribute slogan and update jQuery('#some_other_element') with the slogan value.

### Getting extra object data

If you need to extra data about the selected object, you can use the *:update_elements* HTML attribute.
Expand Down
10 changes: 7 additions & 3 deletions lib/assets/javascripts/autocomplete-rails-uncompressed.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@

jQuery(e).autocomplete({
source: function( request, response ) {
jQuery.getJSON( jQuery(e).attr('data-autocomplete'), {
term: extractLast( request.term )
}, function() {
params = {term: extractLast( request.term )}
if (jQuery(e).attr('data-autocomplete-fields')) {
jQuery.each(jQuery.parseJSON(jQuery(e).attr('data-autocomplete-fields')), function(field, selector) {
params[field] = jQuery(selector).val();
});
}
jQuery.getJSON( jQuery(e).attr('data-autocomplete'), params, function() {
if(arguments[0].length == 0) {
arguments[0] = []
arguments[0][0] = { id: "", label: "no existing match" }
Expand Down
1 change: 1 addition & 0 deletions lib/rails3-jquery-autocomplete/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def autocomplete_field_tag(name, value, source, options ={})
#
private
def rewrite_autocomplete_option(options)
options["data-autocomplete-fields"] = JSON.generate(options.delete :fields) if options[:fields]
options["data-update-elements"] = JSON.generate(options.delete :update_elements) if options[:update_elements]
options["data-id-element"] = options.delete :id_element if options[:id_element]
options
Expand Down
1 change: 1 addition & 0 deletions lib/rails3-jquery-autocomplete/simple_form_plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Autocomplete
#
def rewrite_autocomplete_option
new_options = {}
new_options["data-autocomplete-fields"] = JSON.generate(options.delete :fields) if options[:fields]
new_options["data-update-elements"] = JSON.generate(options.delete :update_elements) if options[:update_elements]
new_options["data-id-element"] = options.delete :id_element if options[:id_element]
input_html_options.merge new_options
Expand Down

0 comments on commit 5e20cd0

Please sign in to comment.