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 support to change or hide label when no matches found #8

Merged
merged 4 commits into from
Jun 12, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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' (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 @@
var _e = e;
this.init(_e);
};
jQuery.railsAutocomplete.options = {
showNoMatches: 'true',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@swparkaust Is there are a reason you are using a string here rather than a bool?

noMatchesLabel: 'no existing match'
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing semicolon.


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 = {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing semicolon.

jQuery.extend(options, jQuery.railsAutocomplete.options);
jQuery.each(options, function(key, value) {
if(options.hasOwnProperty(key)) {
var attr_val = jQuery(e).attr('data-' + key);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Identifier 'attr_val' is not in camel case.

options[key] = attr_val ? attr_val : value;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Identifier 'attr_val' is not in camel case.

}
});
if(arguments[0].length == 0 && options.showNoMatches == 'true') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use '===' to compare with '0'.

arguments[0] = [];
arguments[0][0] = { id: "", label: "no existing match" };
arguments[0][0] = { id: "", label: options.noMatchesLabel };
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mixed double and single quotes.

}
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'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

end