The solution below solves these two problems with
1) Change the autocomplete source function
2) Modifying .data ('autocomplete') ._ renderItem
A working JS script can be found here: http://jsfiddle.net/W8MKt/7/
Any criticism of the code or solution will be appreciated.
Thank.
<div class="ui-widget">
<label for="tags">Multi-word search: </label>
<input id="tags">
</div>
$(function() {
var availableTags = [
"win the day",
"win the heart of",
"win the heart of someone"
];
$( "#tags" ).autocomplete({
source: function(request, response) {
var aryResponse = [];
var arySplitRequest = request.term.split(" ");
for( i = 0; i < availableTags.length; i++ ) {
var intCount = 0;
for( j = 0; j < arySplitRequest.length; j++ ) {
regexp = new RegExp(arySplitRequest[j]);
var test = availableTags[i].match(regexp);
if( test ) {
intCount++;
} else if( !test ) {
intCount = arySplitRequest.length + 1;
}
if ( intCount == arySplitRequest.length ) {
aryResponse.push( availableTags[i] );
}
};
}
response(aryResponse);
}
}).data('autocomplete')._renderItem = function( ul, item ) {
var srchTerm = $.trim(this.term).split(/\s+/).join ('|');
var strNewLabel = item.label;
regexp = new RegExp ('(' + srchTerm + ')', "ig");
var strNewLabel = strNewLabel.replace(regexp,"<span style='font-weight:bold;color:Blue;'>$1</span>");
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + strNewLabel + "</a>" )
.appendTo( ul );
};
});
source
share