Skip to content

Commit d748431

Browse files
committed
Merge pull request #8 from swparkaust/master
Added support to change or hide label when no matches found
2 parents f1c7d3e + 919cb06 commit d748431

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,22 @@ To have the first item be automatically focused on when the autocomplete menu is
240240

241241
Now your autocomplete code is unobtrusive, Rails style.
242242

243+
#### Client-side config
244+
245+
To configure the behaviour if no matches are found, you can set the following options:
246+
247+
jQuery.railsAutocomplete.options.showNoMatches //default true
248+
jQuery.railsAutocomplete.options.noMatchesLabel //default 'no existing match'
249+
250+
These will change the behaviour globally. To set them on a single input field use:
251+
252+
f.autocomplete_field :brand_names, autocomplete_brand_name_products_path,
253+
'data-showNoMatches' => false
254+
#or
255+
f.autocomplete_field :brand_names, autocomplete_brand_name_products_path,
256+
'data-noMatchesLabel' => 'no brands found'
257+
258+
243259
### Getting the object id
244260

245261
If you need to use the id of the selected object, you can use the *id_element* attribute too:

lib/assets/javascripts/autocomplete-rails-uncompressed.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
var _e = e;
3636
this.init(_e);
3737
};
38+
jQuery.railsAutocomplete.options = {
39+
showNoMatches: true,
40+
noMatchesLabel: 'no existing match'
41+
}
3842

3943
jQuery.railsAutocomplete.fn = jQuery.railsAutocomplete.prototype = {
4044
railsAutocomplete: '0.0.1'
@@ -67,9 +71,17 @@
6771
});
6872
}
6973
jQuery.getJSON( jQuery(e).attr('data-autocomplete'), params, function() {
70-
if(arguments[0].length === 0) {
74+
var options = {};
75+
jQuery.extend(options, jQuery.railsAutocomplete.options);
76+
jQuery.each(options, function(key, value) {
77+
if(options.hasOwnProperty(key)) {
78+
var attrVal = jQuery(e).attr('data-' + key);
79+
options[key] = attrVal ? attrVal : value;
80+
}
81+
});
82+
if(arguments[0].length == 0 && options.showNoMatches) {
7183
arguments[0] = [];
72-
arguments[0][0] = { id: "", label: "no existing match" };
84+
arguments[0][0] = { id: "", label: options.noMatchesLabel };
7385
}
7486
jQuery(arguments[0]).each(function(i, el) {
7587
var obj = {};

lib/assets/javascripts/autocomplete-rails.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module RailsJQueryAutocomplete
2-
VERSION = '1.0.0'
2+
VERSION = '1.1.0'
33
end

0 commit comments

Comments
 (0)