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.4.3.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.autocomplete.js"></script>
13
	<link rel="stylesheet" href="../demos.css">
14
	<style>
15
	.ui-button { margin-left: -1px; }
16
	.ui-button-icon-only .ui-button-text { padding: 0.35em; } 
17
	.ui-autocomplete-input { margin: 0; padding: 0.48em 0 0.47em 0.45em; }
18
	</style>
19
	<script>
20
	(function( $ ) {
21
		$.widget( "ui.combobox", {
22
			_create: function() {
23
				var self = this,
24
					select = this.element.hide(),
25
					selected = select.children( ":selected" ),
26
					value = selected.val() ? selected.text() : "";
27
				var input = $( "<input>" )
28
					.insertAfter( select )
29
					.val( value )
30
					.autocomplete({
31
						delay: 0,
32
						minLength: 0,
33
						source: function( request, response ) {
34
							var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
35
							response( select.children( "option" ).map(function() {
36
								var text = $( this ).text();
37
								if ( this.value && ( !request.term || matcher.test(text) ) )
38
									return {
39
										label: text.replace(
40
											new RegExp(
41
												"(?![^&;]+;)(?!<[^<>]*)(" +
42
												$.ui.autocomplete.escapeRegex(request.term) +
43
												")(?![^<>]*>)(?![^&;]+;)", "gi"
44
											), "<strong>$1</strong>" ),
45
										value: text,
46
										option: this
47
									};
48
							}) );
49
						},
50
						select: function( event, ui ) {
51
							ui.item.option.selected = true;
52
							self._trigger( "selected", event, {
53
								item: ui.item.option
54
							});
55
						},
56
						change: function( event, ui ) {
57
							if ( !ui.item ) {
58
								var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
59
									valid = false;
60
								select.children( "option" ).each(function() {
61
									if ( this.value.match( matcher ) ) {
62
										this.selected = valid = true;
63
										return false;
64
									}
65
								});
66
								if ( !valid ) {
67
									// remove invalid value, as it didn't match anything
68
									$( this ).val( "" );
69
									select.val( "" );
70
									return false;
71
								}
72
							}
73
						}
74
					})
75
					.addClass( "ui-widget ui-widget-content ui-corner-left" );
76

    
77
				input.data( "autocomplete" )._renderItem = function( ul, item ) {
78
					return $( "<li></li>" )
79
						.data( "item.autocomplete", item )
80
						.append( "<a>" + item.label + "</a>" )
81
						.appendTo( ul );
82
				};
83

    
84
				$( "<button>&nbsp;</button>" )
85
					.attr( "tabIndex", -1 )
86
					.attr( "title", "Show All Items" )
87
					.insertAfter( input )
88
					.button({
89
						icons: {
90
							primary: "ui-icon-triangle-1-s"
91
						},
92
						text: false
93
					})
94
					.removeClass( "ui-corner-all" )
95
					.addClass( "ui-corner-right ui-button-icon" )
96
					.click(function() {
97
						// close if already visible
98
						if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
99
							input.autocomplete( "close" );
100
							return;
101
						}
102

    
103
						// pass empty string as value to search for, displaying all results
104
						input.autocomplete( "search", "" );
105
						input.focus();
106
					});
107
			}
108
		});
109
	})( jQuery );
110

    
111
	$(function() {
112
		$( "#combobox" ).combobox();
113
		$( "#toggle" ).click(function() {
114
			$( "#combobox" ).toggle();
115
		});
116
	});
117
	</script>
118
</head>
119
<body>
120
	
121
<div class="demo">
122

    
123
<div class="ui-widget">
124
	<label>Your preferred programming language: </label>
125
	<select id="combobox">
126
		<option value="">Select one...</option>
127
		<option value="ActionScript">ActionScript</option>
128
		<option value="AppleScript">AppleScript</option>
129
		<option value="Asp">Asp</option>
130
		<option value="BASIC">BASIC</option>
131
		<option value="C">C</option>
132
		<option value="C++">C++</option>
133
		<option value="Clojure">Clojure</option>
134
		<option value="COBOL">COBOL</option>
135
		<option value="ColdFusion">ColdFusion</option>
136
		<option value="Erlang">Erlang</option>
137
		<option value="Fortran">Fortran</option>
138
		<option value="Groovy">Groovy</option>
139
		<option value="Haskell">Haskell</option>
140
		<option value="Java">Java</option>
141
		<option value="JavaScript">JavaScript</option>
142
		<option value="Lisp">Lisp</option>
143
		<option value="Perl">Perl</option>
144
		<option value="PHP">PHP</option>
145
		<option value="Python">Python</option>
146
		<option value="Ruby">Ruby</option>
147
		<option value="Scala">Scala</option>
148
		<option value="Scheme">Scheme</option>
149
	</select>
150
</div>
151
<button id="toggle">Show underlying select</button>
152

    
153
</div><!-- End demo -->
154

    
155

    
156

    
157
<div class="demo-description">
158
<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>
159
<p>The input is read from an existing select-element for progressive enhancement, passed to Autocomplete with a customized source-option.</p>
160
</div><!-- End demo-description -->
161

    
162
</body>
163
</html>
(2-2/15)