Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added options to customize the behaviour when no matches are found #229

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,22 @@ NOTE: Setting the `:multiple` option to `true` will result in the chosen values

Now your autocomplete code is unobtrusive, Rails 3 style.

#### Client-side config

To configure the behaviour if no matches are found, you can set the following options:

jQuery.railsAutocomplete.options.showNoMatches //default 'true' (yeah, it's a string)
jQuery.railsAutocomplete.options.noMatchesLabel //default 'no existing matches'

These will change the behaviour globally. To set them on a single input field use:

f.autocomplete_field :brand_names, autocomplete_brand_name_products_path,
'data-showNoMatches' => 'false'
#or
f.autocomplete_field :brand_names, autocomplete_brand_name_products_path,
'data-noMatchesLabel' => 'no brands found'


### Getting the object id

If you need to use the id of the selected object, you can use the *id_element* attribute too:
Expand Down
16 changes: 14 additions & 2 deletions lib/assets/javascripts/autocomplete-rails-uncompressed.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
_e = e;
this.init(_e);
};
jQuery.railsAutocomplete.options = {
showNoMatches: 'true',
noMatchesLabel: 'no existing match'
}

jQuery.railsAutocomplete.fn = jQuery.railsAutocomplete.prototype = {
railsAutocomplete: '0.0.1'
Expand All @@ -56,9 +60,17 @@
jQuery.getJSON( jQuery(e).attr('data-autocomplete'), {
term: extractLast( request.term )
}, function() {
if(arguments[0].length == 0) {
var options = {}
jQuery.extend(options, jQuery.railsAutocomplete.options);
jQuery.each(options, function(key, value) {
if(options.hasOwnProperty(key)) {
var attr_val = jQuery(e).attr('data-' + key);
options[key] = attr_val ? attr_val : value;
}
});
if(arguments[0].length == 0 && options.showNoMatches == 'true') {
arguments[0] = []
arguments[0][0] = { id: "", label: "no existing match" }
arguments[0][0] = { id: "", label: options.noMatchesLabel }
}
jQuery(arguments[0]).each(function(i, el) {
var obj = {};
Expand Down
17 changes: 1 addition & 16 deletions lib/assets/javascripts/autocomplete-rails.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/rails3-jquery-autocomplete/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Rails3JQueryAutocomplete
VERSION = '1.0.11'
VERSION = '1.0.13'
end