Skip to content

Commit

Permalink
Merge pull request #8 from swparkaust/master
Browse files Browse the repository at this point in the history
Added support to change or hide label when no matches found
  • Loading branch information
bigtunacan committed Jun 12, 2015
2 parents f1c7d3e + 919cb06 commit d748431
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,22 @@ To have the first item be automatically focused on when the autocomplete menu is

Now your autocomplete code is unobtrusive, Rails 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
jQuery.railsAutocomplete.options.noMatchesLabel //default 'no existing match'

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 @@
var _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 Down Expand Up @@ -67,9 +71,17 @@
});
}
jQuery.getJSON( jQuery(e).attr('data-autocomplete'), params, 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 attrVal = jQuery(e).attr('data-' + key);
options[key] = attrVal ? attrVal : value;
}
});
if(arguments[0].length == 0 && options.showNoMatches) {
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
2 changes: 1 addition & 1 deletion 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/rails-jquery-autocomplete/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module RailsJQueryAutocomplete
VERSION = '1.0.0'
VERSION = '1.1.0'
end

0 comments on commit d748431

Please sign in to comment.