From: Scott González Date: Wed, 21 Jul 2010 18:54:20 +0000 (-0400) Subject: Autocomplete: Added appendTo option. Fixes #5836 - Autocomplete: add appendTo option. X-Git-Tag: 1.8.3~25 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=74e0d4f47301ff854ec741434da1351544a1a55d;p=jquery-ui.git Autocomplete: Added appendTo option. Fixes #5836 - Autocomplete: add appendTo option. --- diff --git a/tests/unit/autocomplete/autocomplete.html b/tests/unit/autocomplete/autocomplete.html index 7ca96eb68..943708694 100644 --- a/tests/unit/autocomplete/autocomplete.html +++ b/tests/unit/autocomplete/autocomplete.html @@ -36,8 +36,8 @@
-
- +
+
diff --git a/tests/unit/autocomplete/autocomplete_defaults.js b/tests/unit/autocomplete/autocomplete_defaults.js index 8dad15e8f..fc92f3209 100644 --- a/tests/unit/autocomplete/autocomplete_defaults.js +++ b/tests/unit/autocomplete/autocomplete_defaults.js @@ -3,6 +3,7 @@ */ var autocomplete_defaults = { + appendTo: "body", delay: 300, disabled: false, minLength: 1, diff --git a/tests/unit/autocomplete/autocomplete_options.js b/tests/unit/autocomplete/autocomplete_options.js index c5aa7c961..ac4aeef35 100644 --- a/tests/unit/autocomplete/autocomplete_options.js +++ b/tests/unit/autocomplete/autocomplete_options.js @@ -68,6 +68,36 @@ test("cache: false", function() { var data = ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby", "python", "c", "scala", "groovy", "haskell", "perl"]; +test( "appendTo", function() { + var ac = $( "#autocomplete" ).autocomplete(); + same( ac.autocomplete( "widget" ).parent()[0], document.body, "defaults to body" ); + ac.autocomplete( "destroy" ); + + ac.autocomplete({ + appendTo: "#ac-wrap2" + }); + same( ac.autocomplete( "widget" ).parent()[0], $( "#ac-wrap2" )[0], "id" ); + ac.autocomplete( "destroy" ); + + ac.autocomplete({ + appendTo: ".ac-wrap" + }); + same( ac.autocomplete( "widget" ).parent()[0], $( "#ac-wrap1" )[0], "class" ); + same( $( "#ac-wrap2 .ui-autocomplete").length, 0, "class - only appends to one element") + ac.autocomplete( "destroy" ); + + ac.autocomplete({ + appendTo: null + }); + same( ac.autocomplete( "widget" ).parent()[0], document.body, "null" ); + ac.autocomplete( "destroy" ); + + ac.autocomplete().autocomplete( "option", "appendTo", "#ac-wrap1" ); + same( ac.autocomplete( "widget" ).parent()[0], $( "#ac-wrap1" )[0], "modified after init" ); + ac.autocomplete( "destroy" ); +}); + + test("delay", function() { var ac = $("#autocomplete").autocomplete({ source: data, diff --git a/ui/jquery.ui.autocomplete.js b/ui/jquery.ui.autocomplete.js index 4b37a000d..0849ba7eb 100644 --- a/ui/jquery.ui.autocomplete.js +++ b/ui/jquery.ui.autocomplete.js @@ -16,6 +16,7 @@ $.widget( "ui.autocomplete", { options: { + appendTo: "body", delay: 300, minLength: 1, position: { @@ -104,7 +105,7 @@ $.widget( "ui.autocomplete", { }; this.menu = $( "" ) .addClass( "ui-autocomplete" ) - .appendTo( "body", doc ) + .appendTo( $( this.options.appendTo || "body", doc )[0] ) // prevent the close-on-blur in case of a "slow" click on the menu (long mousedown) .mousedown(function() { // use another timeout to make sure the blur-event-handler on the input was already triggered @@ -166,11 +167,14 @@ $.widget( "ui.autocomplete", { $.Widget.prototype.destroy.call( this ); }, - _setOption: function( key ) { + _setOption: function( key, value ) { $.Widget.prototype._setOption.apply( this, arguments ); if ( key === "source" ) { this._initSource(); } + if ( key === "appendTo" ) { + this.menu.element.appendTo( $( value || "body", this.element.ownerDocument )[0] ) + } }, _initSource: function() {