I've been banging my head off my desk trying to get the jQuery UI Autocomplete to output custom HTML. Here is my code.
$(document).ready(function(){
$.widget( "custom.catcomplete", $.ui.autocomplete, {
_renderMenu: function( ul, items ) {
var self = this,
currentCategory = "";
$.each( items, function( index, item ) {
if ( item.category != currentCategory ) {
ul.append( "<li class='ui-autocomplete-category'>" + item.category + "<ul class='autocomplete-category'></ul></li>" );
currentCategory = item.category;
}
self._renderItem( ul, item);
});
}
});
var data = [
{ label: "anders", category: "Antigen" },
{ label: "andreas", category: "Antigen" },
{ label: "antal", category: "Antigen" },
{ label: "annhhx10", category: "Products" },
{ label: "annk K12", category: "Products" },
{ label: "annttop C13", category: "Products" },
{ label: "anders andersson", category: "People" },
{ label: "andreas andersson", category: "People" },
{ label: "andreas johnson", category: "People" }
];
$( "#typeAhead" ).catcomplete({
delay: 0,
source: data,
})
.data( "catcomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.catcomplete", item )
.append( $( "<a class='ui-menu-item'></a>" ).text( item.label ) )
.appendTo( $('ul').last('.autocomplete-category'));
};
});
I seem to be running into trouble by nesting my lists in the renderItem function. The HTML output is exactly how I want it. However when I "keydown" the I get a javascript error (item is undefined).
Any ideas or suggestions? I've tried just about everything.