diff --git a/README.md b/README.md index e8c66d70..0accc247 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/assets/javascripts/autocomplete-rails-uncompressed.js b/lib/assets/javascripts/autocomplete-rails-uncompressed.js index 251c8b20..0cfbbc1f 100644 --- a/lib/assets/javascripts/autocomplete-rails-uncompressed.js +++ b/lib/assets/javascripts/autocomplete-rails-uncompressed.js @@ -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" } diff --git a/lib/rails3-jquery-autocomplete/form_helper.rb b/lib/rails3-jquery-autocomplete/form_helper.rb index 006a14df..e18f33cd 100644 --- a/lib/rails3-jquery-autocomplete/form_helper.rb +++ b/lib/rails3-jquery-autocomplete/form_helper.rb @@ -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 diff --git a/lib/rails3-jquery-autocomplete/simple_form_plugin.rb b/lib/rails3-jquery-autocomplete/simple_form_plugin.rb index e76b31d9..c52e2832 100644 --- a/lib/rails3-jquery-autocomplete/simple_form_plugin.rb +++ b/lib/rails3-jquery-autocomplete/simple_form_plugin.rb @@ -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