jQuery UI Combobox Widget Improved
Today I searched for a JQuery UI Combobox. I found a custom widget on the jQuery UI site. By default it populates the combobox off of a select element. I was really wanting to load this off of a Ajax call instead, but only load the top 50 from the DB if they were to click on the arrow to browse. I made some modifications to the Combobox code to allow for the ‘source’ callback which is implemented on their current autocomplete. I also implemented the ‘select’ callback and ‘delay’ property.
I also have it setup to be able to combobox off of a text element if desired, instead of the select.
This widget should go into a js file and be included on the page
(function( $ ) {
$.widget( "ui.combobox", {
_create: function() {
var self = this,
select = this.element.hide(),
selected = select.children( ":selected" ),
value = select.is('input:text') ?
select.val() :
selected.val() ? selected.text() : "";
var input = this.input = $( "" )
.insertAfter( select )
.val( value )
.autocomplete({
delay: self.options.delay ? self.options.delay : 0,
minLength: self.options.minLength ? self.options.minLength : 0,
source: function( request, response ) {
if (!self.options.source) {
var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
response( select.children( "option" ).map(function() {
var text = $( this ).text();
if ( this.value && ( !request.term || matcher.test(text) ) )
return {
label: text.replace(
new RegExp(
"(?![^&;]
(?!<[^<>]*)("
$.ui.autocomplete.escapeRegex(request.term)
")(?![^<>]*>)(?![^&;]
", "gi"
), "$1" ),
value: text,
option: this
};
}) );
} else {
self.options.source(request, response);
}
},
select: function( event, ui ) {
if (!self.options.select) {
ui.item.option.selected = true;
self._trigger( "selected", event, {
item: ui.item.option
});
} else {
self.options.select(event, ui);
}
},
change: function( event, ui ) {
if ( !ui.item ) {
var matcher = new RegExp( "^" $.ui.autocomplete.escapeRegex( $(this).val() ) "$", "i" ),
valid = false;
select.children( "option" ).each(function() {
if ( $( this ).text().match( matcher ) ) {
this.selected = valid = true;
return false;
}
});
if ( !valid ) {
// remove invalid value, as it didn't match anything
$( this ).val( "" );
select.val( "" );
input.data( "autocomplete" ).term = "";
return false;
}
}
}
})
.addClass( "ui-widget ui-widget-content ui-corner-left" );
input.data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "" )
.data( "item.autocomplete", item )
.append( "" item.label "" )
.appendTo( ul );
};
this.button = $( "" )
.attr( "tabIndex", -1 )
.attr( "title", "Show All Items" )
.insertAfter( input )
.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
})
.removeClass( "ui-corner-all" )
.addClass( "ui-corner-right ui-button-icon" )
.click(function() {
// close if already visible
if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
input.autocomplete( "close" );
return;
}
// work around a bug (likely same cause as #5265)
$( this ).blur();
// pass empty string as value to search for, displaying all results
input.autocomplete( "search", "" );
input.focus();
}).find('.ui-button-text').removeClass('ui-button-text'); //added the remove ui-button-text to remove the padding
//and make it the same size as our input
//see button in Message Center -> New Message for reference
},
destroy: function() {
this.input.remove();
this.button.remove();
this.element.show();
$.Widget.prototype.destroy.call( this );
}
});
})( jQuery );
Here is an example usage of the widget:
$('#myTextField').combobox({
source: function(req,add) {
$.ajax({
url: "test.html",
dataType:"json",
success: function(data, textStatus, XMLHttpRequest){
add(data);
}
});
},
select: function(event, ui) {
//call back when the user selects an item on the list
alert('Item id selected [' ui.item.id ']');
},
delay: 500
});
Here is an example of json structure that the add() method takes above
[{"id":"1","label":"Option1"},{"id":"2","label":"Option2"},{"id":"3","label":"Option3"}]
This mod does not break the traditional means of creating the combobox off of a select element
$( "#MySelectElement" ).combobox();