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

ShowNoSuggestionNotice can be a function. Fix of noSuggestionNotice. #341

Open
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ The standard jquery.autocomplete.js file is around 2.7KB when minified via Closu
* `autoSelectFirst`: if set to `true`, first item will be selected when showing suggestions. Default value `false`.
* `appendTo`: container where suggestions will be appended. Default value `document.body`. Can be jQuery object, selector or html element. Make sure to set `position: absolute` or `position: relative` for that element.
* `dataType`: type of data returned from server. Either 'text' (default) or 'jsonp', which will cause the autocomplete to use jsonp. You may return a json object in your callback when using jsonp.
* `showNoSuggestionNotice`: Default `false`. When no matching results, display a notification label.
* `showNoSuggestionNotice`: Default `false`. Boolean or `function (suggestions) {}`. When set `true` and no matching results, display a notification label. When function given, it is called on every suggestion display and if `true` returned, notification is displayed.
* `noSuggestionNotice`: Default `No results`. Text or htmlString or Element or jQuery object for no matching results label.
* `forceFixPosition`: Default: `false`. Suggestions are automatically positioned when their container is appended to body (look at `appendTo` option), in other cases suggestions are rendered but no positioning is applied.
Set this option to force auto positioning in other cases.
Expand Down
25 changes: 20 additions & 5 deletions src/jquery.autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
suggestionSelector = '.' + that.classes.suggestion,
selected = that.classes.selected,
options = that.options,
noSuggestionNotice = this.options.noSuggestionNotice,
container;

// Remove autocomplete attribute to prevent native suggestions:
Expand All @@ -159,9 +160,12 @@
}
};

// if notice is not string, it should be deep-copied, so every autocomplete instance has its own copy
if(typeof noSuggestionNotice !== 'string')
noSuggestionNotice = $(noSuggestionNotice).clone(true);
// html() deals with many types: htmlString or Element or Array or jQuery
that.noSuggestionsContainer = $('<div class="autocomplete-no-suggestion"></div>')
.html(this.options.noSuggestionNotice).get(0);
.html(noSuggestionNotice).get(0);

that.suggestionsContainer = Autocomplete.utils.createNode(options.containerClass);

Expand Down Expand Up @@ -208,7 +212,6 @@

onFocus: function () {
var that = this;
that.fixPosition();
if (that.options.minChars <= that.el.val().length) {
that.onValueChange();
}
Expand Down Expand Up @@ -634,11 +637,15 @@

suggest: function () {
if (this.suggestions.length === 0) {
if (this.options.showNoSuggestionNotice) {

var showNoSuggestionNotice = this.options.showNoSuggestionNotice;
if(typeof this.options.showNoSuggestionNotice === 'function')
showNoSuggestionNotice = this.options.showNoSuggestionNotice(this.suggestions);

if(showNoSuggestionNotice)
this.noSuggestions();
} else {
else
this.hide();
}
return;
}

Expand Down Expand Up @@ -686,9 +693,17 @@

this.adjustContainerWidth();

// Detach noSuggestions not to have it removed when filling container with new suggestions
noSuggestionsContainer.detach();
container.html(html);

// If showNoSuggestionNotice is a function, call it to see
// if noSuggestionNotice should be added to theses suggestions
if(typeof this.options.showNoSuggestionNotice === 'function'
&& this.options.showNoSuggestionNotice(that.suggestions)) {
container.append(noSuggestionsContainer);
}

if ($.isFunction(beforeRender)) {
beforeRender.call(that.element, container);
}
Expand Down