From a40d1a9d658d7cf3e13cb33448b7af5c5b9645d3 Mon Sep 17 00:00:00 2001 From: Uwe Kubosch Date: Thu, 31 May 2012 12:57:05 +0200 Subject: [PATCH] * Add option to send additional fields with the auto complete query. --- .../javascripts/autocomplete-rails-uncompressed.js | 11 ++++++++--- lib/rails3-jquery-autocomplete/form_helper.rb | 1 + 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/assets/javascripts/autocomplete-rails-uncompressed.js b/lib/assets/javascripts/autocomplete-rails-uncompressed.js index 5b23fbf5..1a752cba 100644 --- a/lib/assets/javascripts/autocomplete-rails-uncompressed.js +++ b/lib/assets/javascripts/autocomplete-rails-uncompressed.js @@ -47,9 +47,14 @@ jQuery(e).autocomplete({ source: function( request, response ) { - jQuery.getJSON( jQuery(e).attr('data-autocomplete'), { - term: extractLast( request.term ) - }, function() { + var queryFields = {term: extractLast( request.term )}; + var fieldsAttr = jQuery(e).attr('data-autocomplete-fields'); + if (fieldsAttr != null) { + var fields = JSON.parse(fieldsAttr); + for(var k in fields) queryFields[k] = jQuery(fields[k]).val(); + } + jQuery.getJSON( jQuery(e).attr('data-autocomplete'), queryFields, + function() { jQuery(arguments[0]).each(function(i, el) { var obj = {}; obj[el.id] = el; diff --git a/lib/rails3-jquery-autocomplete/form_helper.rb b/lib/rails3-jquery-autocomplete/form_helper.rb index 006a14df..5cc6d824 100644 --- a/lib/rails3-jquery-autocomplete/form_helper.rb +++ b/lib/rails3-jquery-autocomplete/form_helper.rb @@ -35,6 +35,7 @@ def autocomplete_field_tag(name, value, source, options ={}) def rewrite_autocomplete_option(options) 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["data-autocomplete-fields"] = JSON.generate(options.delete :fields) if options[:fields] options end end