client/player/development-bundle/demos/autocomplete/combobox.html
changeset 50 1ecfe4720da7
parent 49 af3778eab5e8
child 51 2d6866072851
equal deleted inserted replaced
49:af3778eab5e8 50:1ecfe4720da7
     1 <!DOCTYPE html>
       
     2 <html lang="en">
       
     3 <head>
       
     4 	<meta charset="UTF-8" />
       
     5 	<title>jQuery UI Autocomplete Combobox Demo</title>
       
     6 	<link type="text/css" href="../../themes/base/jquery.ui.all.css" rel="stylesheet" />
       
     7 	<script type="text/javascript" src="../../jquery-1.4.2.js"></script>
       
     8 	<script type="text/javascript" src="../../ui/jquery.ui.core.js"></script>
       
     9 	<script type="text/javascript" src="../../ui/jquery.ui.widget.js"></script>
       
    10 	<script type="text/javascript" src="../../ui/jquery.ui.button.js"></script>
       
    11 	<script type="text/javascript" src="../../ui/jquery.ui.position.js"></script>
       
    12 	<script type="text/javascript" src="../../ui/jquery.ui.autocomplete.js"></script>
       
    13 	<link type="text/css" href="../demos.css" rel="stylesheet" />
       
    14 	<style type="text/css">
       
    15 		/* TODO shouldn't be necessary */
       
    16 		.ui-button { margin-left: -1px; }
       
    17 		.ui-button-icon-only .ui-button-text { padding: 0.35em; } 
       
    18 		.ui-autocomplete-input { margin: 0; padding: 0.48em 0 0.47em 0.45em; }
       
    19 	</style>
       
    20 	<script type="text/javascript">
       
    21 	(function($) {
       
    22 		$.widget("ui.combobox", {
       
    23 			_create: function() {
       
    24 				var self = this;
       
    25 				var select = this.element.hide();
       
    26 				var input = $("<input>")
       
    27 					.insertAfter(select)
       
    28 					.autocomplete({
       
    29 						source: function(request, response) {
       
    30 							var matcher = new RegExp(request.term, "i");
       
    31 							response(select.children("option").map(function() {
       
    32 								var text = $(this).text();
       
    33 								if (this.value && (!request.term || matcher.test(text)))
       
    34 									return {
       
    35 										id: this.value,
       
    36 										label: text.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + $.ui.autocomplete.escapeRegex(request.term) + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>"),
       
    37 										value: text
       
    38 									};
       
    39 							}));
       
    40 						},
       
    41 						delay: 0,
       
    42 						change: function(event, ui) {
       
    43 							if (!ui.item) {
       
    44 								// remove invalid value, as it didn't match anything
       
    45 								$(this).val("");
       
    46 								return false;
       
    47 							}
       
    48 							select.val(ui.item.id);
       
    49 							self._trigger("selected", event, {
       
    50 								item: select.find("[value='" + ui.item.id + "']")
       
    51 							});
       
    52 							
       
    53 						},
       
    54 						minLength: 0
       
    55 					})
       
    56 					.addClass("ui-widget ui-widget-content ui-corner-left");
       
    57 				$("<button>&nbsp;</button>")
       
    58 				.attr("tabIndex", -1)
       
    59 				.attr("title", "Show All Items")
       
    60 				.insertAfter(input)
       
    61 				.button({
       
    62 					icons: {
       
    63 						primary: "ui-icon-triangle-1-s"
       
    64 					},
       
    65 					text: false
       
    66 				}).removeClass("ui-corner-all")
       
    67 				.addClass("ui-corner-right ui-button-icon")
       
    68 				.click(function() {
       
    69 					// close if already visible
       
    70 					if (input.autocomplete("widget").is(":visible")) {
       
    71 						input.autocomplete("close");
       
    72 						return;
       
    73 					}
       
    74 					// pass empty string as value to search for, displaying all results
       
    75 					input.autocomplete("search", "");
       
    76 					input.focus();
       
    77 				});
       
    78 			}
       
    79 		});
       
    80 
       
    81 	})(jQuery);
       
    82 		
       
    83 	$(function() {
       
    84 		$("#combobox").combobox();
       
    85 		$("#toggle").click(function() {
       
    86 			$("#combobox").toggle();
       
    87 		});
       
    88 	});
       
    89 	</script>
       
    90 </head>
       
    91 <body>
       
    92 	
       
    93 <div class="demo">
       
    94 
       
    95 <div class="ui-widget">
       
    96 	<label>Your preferred programming language: </label>
       
    97 	<select id="combobox">
       
    98 		<option value="">Select one...</option>
       
    99 		<option value="a">asp</option>
       
   100         <option value="c">c</option>
       
   101         <option value="cpp">c++</option>
       
   102         <option value="cf">coldfusion</option>
       
   103         <option value="g">groovy</option>
       
   104         <option value="h">haskell</option>
       
   105         <option value="j">java</option>
       
   106         <option value="js">javascript</option>
       
   107         <option value="p1">perl</option>
       
   108         <option value="p2">php</option>
       
   109         <option value="p3">python</option>
       
   110         <option value="r">ruby</option>
       
   111         <option value="s">scala</option>
       
   112 	</select>
       
   113 </div>
       
   114 <button id="toggle">Show underlying select</button>
       
   115 
       
   116 </div><!-- End demo -->
       
   117 
       
   118 <div class="demo-description">
       
   119 <p>
       
   120 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.
       
   121 </p>
       
   122 <p>
       
   123 The input is read from an existing select-element for progressive enhancement, passed to Autocomplete with a customized source-option.
       
   124 </p>
       
   125 </div><!-- End demo-description -->
       
   126 
       
   127 </body>
       
   128 </html>