Project

General

Profile

1
<!doctype html>
2
<html lang="en">
3
<head>
4
	<meta charset="utf-8">
5
	<title>jQuery UI Autocomplete - Combobox</title>
6
	<link rel="stylesheet" href="../../themes/base/jquery.ui.all.css">
7
	<script src="../../jquery-1.8.2.js"></script>
8
	<script src="../../ui/jquery.ui.core.js"></script>
9
	<script src="../../ui/jquery.ui.widget.js"></script>
10
	<script src="../../ui/jquery.ui.button.js"></script>
11
	<script src="../../ui/jquery.ui.position.js"></script>
12
	<script src="../../ui/jquery.ui.menu.js"></script>
13
	<script src="../../ui/jquery.ui.autocomplete.js"></script>
14
	<script src="../../ui/jquery.ui.tooltip.js"></script>
15
	<link rel="stylesheet" href="../demos.css">
16
	<style>
17
	.ui-combobox {
18
		position: relative;
19
		display: inline-block;
20
	}
21
	.ui-combobox-toggle {
22
		position: absolute;
23
		top: 0;
24
		bottom: 0;
25
		margin-left: -1px;
26
		padding: 0;
27
		/* adjust styles for IE 6/7 */
28
		*height: 1.7em;
29
		*top: 0.1em;
30
	}
31
	.ui-combobox-input {
32
		margin: 0;
33
		padding: 0.3em;
34
	}
35
	</style>
36
	<script>
37
	(function( $ ) {
38
		$.widget( "ui.combobox", {
39
			_create: function() {
40
				var input,
41
					that = this,
42
					select = this.element.hide(),
43
					selected = select.children( ":selected" ),
44
					value = selected.val() ? selected.text() : "",
45
					wrapper = this.wrapper = $( "<span>" )
46
						.addClass( "ui-combobox" )
47
						.insertAfter( select );
48

    
49
				function removeIfInvalid(element) {
50
					var value = $( element ).val(),
51
						matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( value ) + "$", "i" ),
52
						valid = false;
53
					select.children( "option" ).each(function() {
54
						if ( $( this ).text().match( matcher ) ) {
55
							this.selected = valid = true;
56
							return false;
57
						}
58
					});
59
					if ( !valid ) {
60
						// remove invalid value, as it didn't match anything
61
						$( element )
62
							.val( "" )
63
							.attr( "title", value + " didn't match any item" )
64
							.tooltip( "open" );
65
						select.val( "" );
66
						setTimeout(function() {
67
							input.tooltip( "close" ).attr( "title", "" );
68
						}, 2500 );
69
						input.data( "autocomplete" ).term = "";
70
						return false;
71
					}
72
				}
73

    
74
				input = $( "<input>" )
75
					.appendTo( wrapper )
76
					.val( value )
77
					.attr( "title", "" )
78
					.addClass( "ui-state-default ui-combobox-input" )
79
					.autocomplete({
80
						delay: 0,
81
						minLength: 0,
82
						source: function( request, response ) {
83
							var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
84
							response( select.children( "option" ).map(function() {
85
								var text = $( this ).text();
86
								if ( this.value && ( !request.term || matcher.test(text) ) )
87
									return {
88
										label: text.replace(
89
											new RegExp(
90
												"(?![^&;]+;)(?!<[^<>]*)(" +
91
												$.ui.autocomplete.escapeRegex(request.term) +
92
												")(?![^<>]*>)(?![^&;]+;)", "gi"
93
											), "<strong>$1</strong>" ),
94
										value: text,
95
										option: this
96
									};
97
							}) );
98
						},
99
						select: function( event, ui ) {
100
							ui.item.option.selected = true;
101
							that._trigger( "selected", event, {
102
								item: ui.item.option
103
							});
104
						},
105
						change: function( event, ui ) {
106
							if ( !ui.item )
107
								return removeIfInvalid( this );
108
						}
109
					})
110
					.addClass( "ui-widget ui-widget-content ui-corner-left" );
111

    
112
				input.data( "autocomplete" )._renderItem = function( ul, item ) {
113
					return $( "<li>" )
114
						.data( "item.autocomplete", item )
115
						.append( "<a>" + item.label + "</a>" )
116
						.appendTo( ul );
117
				};
118

    
119
				$( "<a>" )
120
					.attr( "tabIndex", -1 )
121
					.attr( "title", "Show All Items" )
122
					.tooltip()
123
					.appendTo( wrapper )
124
					.button({
125
						icons: {
126
							primary: "ui-icon-triangle-1-s"
127
						},
128
						text: false
129
					})
130
					.removeClass( "ui-corner-all" )
131
					.addClass( "ui-corner-right ui-combobox-toggle" )
132
					.click(function() {
133
						// close if already visible
134
						if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
135
							input.autocomplete( "close" );
136
							removeIfInvalid( input );
137
							return;
138
						}
139

    
140
						// work around a bug (likely same cause as #5265)
141
						$( this ).blur();
142

    
143
						// pass empty string as value to search for, displaying all results
144
						input.autocomplete( "search", "" );
145
						input.focus();
146
					});
147

    
148
					input
149
						.tooltip({
150
							position: {
151
								of: this.button
152
							},
153
							tooltipClass: "ui-state-highlight"
154
						});
155
			},
156

    
157
			destroy: function() {
158
				this.wrapper.remove();
159
				this.element.show();
160
				$.Widget.prototype.destroy.call( this );
161
			}
162
		});
163
	})( jQuery );
164

    
165
	$(function() {
166
		$( "#combobox" ).combobox();
167
		$( "#toggle" ).click(function() {
168
			$( "#combobox" ).toggle();
169
		});
170
	});
171
	</script>
172
</head>
173
<body>
174

    
175
<div class="ui-widget">
176
	<label>Your preferred programming language: </label>
177
	<select id="combobox">
178
		<option value="">Select one...</option>
179
		<option value="ActionScript">ActionScript</option>
180
		<option value="AppleScript">AppleScript</option>
181
		<option value="Asp">Asp</option>
182
		<option value="BASIC">BASIC</option>
183
		<option value="C">C</option>
184
		<option value="C++">C++</option>
185
		<option value="Clojure">Clojure</option>
186
		<option value="COBOL">COBOL</option>
187
		<option value="ColdFusion">ColdFusion</option>
188
		<option value="Erlang">Erlang</option>
189
		<option value="Fortran">Fortran</option>
190
		<option value="Groovy">Groovy</option>
191
		<option value="Haskell">Haskell</option>
192
		<option value="Java">Java</option>
193
		<option value="JavaScript">JavaScript</option>
194
		<option value="Lisp">Lisp</option>
195
		<option value="Perl">Perl</option>
196
		<option value="PHP">PHP</option>
197
		<option value="Python">Python</option>
198
		<option value="Ruby">Ruby</option>
199
		<option value="Scala">Scala</option>
200
		<option value="Scheme">Scheme</option>
201
	</select>
202
</div>
203
<button id="toggle">Show underlying select</button>
204

    
205
<div class="demo-description">
206
<p>A custom widget built by composition of Autocomplete and Button. You can either type something into the field to get filtered suggestions based on your input, or use the button to get the full list of selections.</p>
207
<p>The input is read from an existing select-element for progressive enhancement, passed to Autocomplete with a customized source-option.</p>
208
<p>This is not a supported or even complete widget. Its purely for demoing what autocomplete can do with a bit of customization. <a href="http://www.learningjquery.com/2010/06/a-jquery-ui-combobox-under-the-hood">For a detailed explanation of how the widget works, check out this Learning jQuery article.</a></p>
209
</div>
210
</body>
211
</html>
(2-2/15)