Skip to content

Commit 562f152

Browse files
committed
noSuggestions support extended to text, htmlString, Element and jQuery.
1 parent f69a9d0 commit 562f152

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ The standard jquery.autocomplete.js file is around 2.7KB when minified via Closu
4242
* `paramName`: Default `query`. The name of the request parameter that contains the query.
4343
* `transformResult`: `function(response, originalQuery) {}` called after the result of the query is ready. Converts the result into response.suggestions format.
4444
* `autoSelectFirst`: if set to `true`, first item will be selected when showing suggestions. Default value `false`.
45-
* `appendTo`: container where suggestions will be appended. Default value `body`. Can be jQuery object, selector or html element. Make sure to set `position: absolute` or `position: relative` for that element.
45+
* `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.
4646
* `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.
4747
* `showNoSuggestionNotice`: Default `false`. When no matching results, display a notification label.
48-
* `noSuggestionNotice`: Default `No results`. Text for no matching results label.
48+
* `noSuggestionNotice`: Default `No results`. Text or htmlString or Element or jQuery object for no matching results label.
4949
* `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.
5050
Set this option to force auto positioning in other cases.
5151
* `orientation`: Default `bottom`. Vertical orientation of the displayed suggestions, available values are `auto`, `top`, `bottom`.

src/jquery.autocomplete.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
that = this,
5555
defaults = {
5656
autoSelectFirst: false,
57-
appendTo: 'body',
57+
appendTo: document.body,
5858
serviceUrl: null,
5959
lookup: null,
6060
onSelect: null,
@@ -103,6 +103,7 @@
103103
that.onChange = null;
104104
that.isLocal = false;
105105
that.suggestionsContainer = null;
106+
that.noSuggestionsContainer = null;
106107
that.options = $.extend({}, defaults, options);
107108
that.classes = {
108109
selected: 'autocomplete-selected',
@@ -136,7 +137,8 @@
136137
suggestionSelector = '.' + that.classes.suggestion,
137138
selected = that.classes.selected,
138139
options = that.options,
139-
container;
140+
container,
141+
noSuggestionsContainer;
140142

141143
// Remove autocomplete attribute to prevent native suggestions:
142144
that.element.setAttribute('autocomplete', 'off');
@@ -148,6 +150,10 @@
148150
}
149151
};
150152

153+
// html() deals with many types: htmlString or Element or Array or jQuery
154+
that.noSuggestionsContainer = $('<div class="autocomplete-no-suggestion"></div>')
155+
.html(this.options.noSuggestionNotice).get(0);
156+
151157
that.suggestionsContainer = Autocomplete.utils.createNode(options.containerClass);
152158

153159
container = $(that.suggestionsContainer);
@@ -249,10 +255,11 @@
249255
},
250256

251257
fixPosition: function () {
258+
// Use only when container has already its content
259+
252260
var that = this,
253261
$container = $(that.suggestionsContainer),
254262
containerParent = $container.parent().get(0);
255-
256263
// Fix position automatically when appended to body.
257264
// In other cases force parameter must be given.
258265
if (containerParent !== document.body && !that.options.forceFixPosition)
@@ -594,6 +601,7 @@
594601
className = that.classes.suggestion,
595602
classSelected = that.classes.selected,
596603
container = $(that.suggestionsContainer),
604+
noSuggestionsContainer = $(that.noSuggestionsContainer),
597605
beforeRender = options.beforeRender,
598606
html = '',
599607
index,
@@ -614,6 +622,7 @@
614622

615623
this.adjustContainerWidth();
616624

625+
noSuggestionsContainer.detach();
617626
container.html(html);
618627

619628
// Select first value by default:
@@ -636,15 +645,17 @@
636645

637646
noSuggestions: function() {
638647
var that = this,
639-
container = $(that.suggestionsContainer),
640-
html = '',
641-
width;
642-
643-
html += '<div class="autocomplete-no-suggestion">' + this.options.noSuggestionNotice + '</div>';
648+
container = $(that.suggestionsContainer),
649+
noSuggestionsContainer = $(that.noSuggestionsContainer);
644650

645651
this.adjustContainerWidth();
646-
container.html(html);
647-
652+
653+
// Some explicit steps. Be careful here as it easy to get
654+
// noSuggestionsContainer removed from DOM if not detached properly.
655+
noSuggestionsContainer.detach();
656+
container.empty(); // clean suggestions if any
657+
container.append(noSuggestionsContainer);
658+
648659
that.fixPosition();
649660

650661
container.show();
@@ -655,7 +666,7 @@
655666
var that = this,
656667
options = that.options,
657668
width,
658-
container = $(that.suggestionsContainer)
669+
container = $(that.suggestionsContainer);
659670

660671
// If width is auto, adjust width before displaying suggestions,
661672
// because if instance was created before input had width, it will be zero.
@@ -878,7 +889,7 @@
878889
};
879890

880891
// Create chainable jQuery plugin:
881-
$.fn.autocomplete = function (options, args) {
892+
$.fn.autocomplete = function (options, args) {
882893
var dataKey = 'autocomplete';
883894
// If function invoked without argument return
884895
// instance of the first matched element:

0 commit comments

Comments
 (0)