|
54 | 54 | that = this, |
55 | 55 | defaults = { |
56 | 56 | autoSelectFirst: false, |
57 | | - appendTo: 'body', |
| 57 | + appendTo: document.body, |
58 | 58 | serviceUrl: null, |
59 | 59 | lookup: null, |
60 | 60 | onSelect: null, |
|
103 | 103 | that.onChange = null; |
104 | 104 | that.isLocal = false; |
105 | 105 | that.suggestionsContainer = null; |
| 106 | + that.noSuggestionsContainer = null; |
106 | 107 | that.options = $.extend({}, defaults, options); |
107 | 108 | that.classes = { |
108 | 109 | selected: 'autocomplete-selected', |
|
136 | 137 | suggestionSelector = '.' + that.classes.suggestion, |
137 | 138 | selected = that.classes.selected, |
138 | 139 | options = that.options, |
139 | | - container; |
| 140 | + container, |
| 141 | + noSuggestionsContainer; |
140 | 142 |
|
141 | 143 | // Remove autocomplete attribute to prevent native suggestions: |
142 | 144 | that.element.setAttribute('autocomplete', 'off'); |
|
148 | 150 | } |
149 | 151 | }; |
150 | 152 |
|
| 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 | + |
151 | 157 | that.suggestionsContainer = Autocomplete.utils.createNode(options.containerClass); |
152 | 158 |
|
153 | 159 | container = $(that.suggestionsContainer); |
|
249 | 255 | }, |
250 | 256 |
|
251 | 257 | fixPosition: function () { |
| 258 | + // Use only when container has already its content |
| 259 | + |
252 | 260 | var that = this, |
253 | 261 | $container = $(that.suggestionsContainer), |
254 | 262 | containerParent = $container.parent().get(0); |
255 | | - |
256 | 263 | // Fix position automatically when appended to body. |
257 | 264 | // In other cases force parameter must be given. |
258 | 265 | if (containerParent !== document.body && !that.options.forceFixPosition) |
|
594 | 601 | className = that.classes.suggestion, |
595 | 602 | classSelected = that.classes.selected, |
596 | 603 | container = $(that.suggestionsContainer), |
| 604 | + noSuggestionsContainer = $(that.noSuggestionsContainer), |
597 | 605 | beforeRender = options.beforeRender, |
598 | 606 | html = '', |
599 | 607 | index, |
|
614 | 622 |
|
615 | 623 | this.adjustContainerWidth(); |
616 | 624 |
|
| 625 | + noSuggestionsContainer.detach(); |
617 | 626 | container.html(html); |
618 | 627 |
|
619 | 628 | // Select first value by default: |
|
636 | 645 |
|
637 | 646 | noSuggestions: function() { |
638 | 647 | 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); |
644 | 650 |
|
645 | 651 | 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 | + |
648 | 659 | that.fixPosition(); |
649 | 660 |
|
650 | 661 | container.show(); |
|
655 | 666 | var that = this, |
656 | 667 | options = that.options, |
657 | 668 | width, |
658 | | - container = $(that.suggestionsContainer) |
| 669 | + container = $(that.suggestionsContainer); |
659 | 670 |
|
660 | 671 | // If width is auto, adjust width before displaying suggestions, |
661 | 672 | // because if instance was created before input had width, it will be zero. |
|
878 | 889 | }; |
879 | 890 |
|
880 | 891 | // Create chainable jQuery plugin: |
881 | | - $.fn.autocomplete = function (options, args) { |
| 892 | + $.fn.autocomplete = function (options, args) { |
882 | 893 | var dataKey = 'autocomplete'; |
883 | 894 | // If function invoked without argument return |
884 | 895 | // instance of the first matched element: |
|
0 commit comments