Skip to content

Commit

Permalink
Merge pull request #250 from DatekWireless/master
Browse files Browse the repository at this point in the history
Fixes #249 Enable setting check boxes...
  • Loading branch information
bigtunacan committed Mar 20, 2014
2 parents 8e9f3c8 + 5e20cd0 commit 0a6e4ae
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,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
31 changes: 25 additions & 6 deletions lib/assets/javascripts/autocomplete-rails-uncompressed.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,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 All @@ -70,7 +74,8 @@
});
},
change: function( event, ui ) {
if(jQuery(jQuery(this).attr('data-id-element')).val() == "") {
if(!jQuery(this).is('[data-id-element]') ||
jQuery(jQuery(this).attr('data-id-element')).val() == "") {
return;
}
jQuery(jQuery(this).attr('data-id-element')).val(ui.item ? ui.item.id : "");
Expand All @@ -82,7 +87,14 @@
return;
}
for (var key in update_elements) {
jQuery(update_elements[key]).val(ui.item ? data[key] : "");
element = jQuery(update_elements[key]);
if (element.is(':checkbox')) {
if (data[key] != null) {
element.prop('checked', data[key]);
}
} else {
element.val(ui.item ? data[key] : "");
}
}
}
},
Expand Down Expand Up @@ -116,7 +128,14 @@
var data = jQuery(this).data(ui.item.id.toString());
var update_elements = jQuery.parseJSON(jQuery(this).attr("data-update-elements"));
for (var key in update_elements) {
jQuery(update_elements[key]).val(data[key]);
element = jQuery(update_elements[key]);
if (element.is(':checkbox')) {
if (data[key] != null) {
element.prop('checked', data[key]);
}
} else {
element.val(data[key]);
}
}
}
}
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 0a6e4ae

Please sign in to comment.