Skip to content

Commit ba09165

Browse files
committed
Autcomplete: Refactored combobox demo.
1 parent c2c09e8 commit ba09165

File tree

1 file changed

+60
-50
lines changed

1 file changed

+60
-50
lines changed

demos/autocomplete/combobox.html

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,75 +18,85 @@
1818
.ui-autocomplete-input { margin: 0; padding: 0.48em 0 0.47em 0.45em; }
1919
</style>
2020
<script type="text/javascript">
21-
(function($) {
22-
$.widget("ui.combobox", {
21+
(function( $ ) {
22+
$.widget( "ui.combobox", {
2323
_create: function() {
2424
var self = this;
2525
var select = this.element.hide();
26-
var input = $("<input>")
27-
.insertAfter(select)
26+
var input = $( "<input>" )
27+
.insertAfter( select )
2828
.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)))
29+
delay: 0,
30+
minLength: 0,
31+
source: function( request, response ) {
32+
var matcher = new RegExp( request.term, "i" );
33+
response( select.children( "option" ).map(function() {
34+
var text = $( this ).text();
35+
if ( this.value && ( !request.term || matcher.test(text) ) )
3436
return {
35-
id: this.value,
36-
label: text.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + $.ui.autocomplete.escapeRegex(request.term) + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>"),
37-
value: text
37+
label: text.replace(
38+
new RegExp(
39+
"(?![^&;]+;)(?!<[^<>]*)(" +
40+
$.ui.autocomplete.escapeRegex(request.term) +
41+
")(?![^<>]*>)(?![^&;]+;)", "gi"
42+
), "<strong>$1</strong>" ),
43+
value: text,
44+
option: this
3845
};
39-
}));
46+
}) );
4047
},
41-
delay: 0,
42-
change: function(event, ui) {
43-
if (!ui.item) {
48+
select: function( event, ui ) {
49+
ui.item.option.selected = true;
50+
//select.val( ui.item.option.value );
51+
self._trigger( "selected", event, {
52+
item: ui.item.option
53+
});
54+
},
55+
change: function( event, ui ) {
56+
if ( !ui.item ) {
4457
// remove invalid value, as it didn't match anything
45-
$(this).val("");
46-
select.val("");
58+
$( this ).val( "" );
59+
select.val( "" );
4760
return false;
4861
}
49-
select.val(ui.item.id);
50-
self._trigger("selected", event, {
51-
item: select.find("[value='" + ui.item.id + "']")
52-
});
53-
54-
},
55-
minLength: 0
62+
}
5663
})
57-
.addClass("ui-widget ui-widget-content ui-corner-left");
58-
input.data("autocomplete")._renderItem = function( ul, item) {
64+
.addClass( "ui-widget ui-widget-content ui-corner-left" );
65+
66+
input.data( "autocomplete" )._renderItem = function( ul, item ) {
5967
return $( "<li></li>" )
6068
.data( "item.autocomplete", item )
6169
.append( "<a>" + item.label + "</a>" )
6270
.appendTo( ul );
6371
};
64-
$("<button>&nbsp;</button>")
65-
.attr("tabIndex", -1)
66-
.attr("title", "Show All Items")
67-
.insertAfter(input)
68-
.button({
69-
icons: {
70-
primary: "ui-icon-triangle-1-s"
71-
},
72-
text: false
73-
}).removeClass("ui-corner-all")
74-
.addClass("ui-corner-right ui-button-icon")
75-
.click(function() {
76-
// close if already visible
77-
if (input.autocomplete("widget").is(":visible")) {
78-
input.autocomplete("close");
79-
return;
80-
}
81-
// pass empty string as value to search for, displaying all results
82-
input.autocomplete("search", "");
83-
input.focus();
84-
});
72+
73+
$( "<button>&nbsp;</button>" )
74+
.attr( "tabIndex", -1 )
75+
.attr( "title", "Show All Items" )
76+
.insertAfter( input )
77+
.button({
78+
icons: {
79+
primary: "ui-icon-triangle-1-s"
80+
},
81+
text: false
82+
})
83+
.removeClass( "ui-corner-all" )
84+
.addClass( "ui-corner-right ui-button-icon" )
85+
.click(function() {
86+
// close if already visible
87+
if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
88+
input.autocomplete( "close" );
89+
return;
90+
}
91+
92+
// pass empty string as value to search for, displaying all results
93+
input.autocomplete( "search", "" );
94+
input.focus();
95+
});
8596
}
8697
});
87-
8898
})(jQuery);
89-
99+
90100
$(function() {
91101
$("#combobox").combobox();
92102
$("#toggle").click(function() {

0 commit comments

Comments
 (0)