Skip to content

Instantly share code, notes, and snippets.

@aabir
Created February 4, 2015 12:37
Show Gist options
  • Save aabir/34c1a4508d0a79db67b5 to your computer and use it in GitHub Desktop.
Save aabir/34c1a4508d0a79db67b5 to your computer and use it in GitHub Desktop.
Jquery auto complete with categories and image result.
jQuery.widget( "custom.catcomplete", $.ui.autocomplete, {
_create: function() {
this._super();
this.widget().menu( "option", "items", "> :not(.ui-autocomplete-category)" );
},
_renderMenu: function( ul, items ) {
var that = this,
currentCategory = "";
$.each( items, function( index, item ) {
var li;
if ( item.category != currentCategory ) {
ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
currentCategory = item.category;
}
li = that._renderItemData( ul, item );
if ( item.category ) {
li.attr( "aria-label", item.category + " : " + item.label );
}
});
},
_renderItem: function( ul, item ) {
var $li = $('<li>'),
$img = $('<img>');
$img.attr({
src: 'http://recolitus.com/zkyon/img/' + item.icon,
//src: 'http://localhost/zkyon/img/' + item.icon,
alt: item.label
});
$li.attr('data-value', item.label);
$li.append('<a href="#">');
$li.find('a').append($img).append(item.label);
return $li.appendTo(ul);
}
});
jQuery(function() {
var data = [
{ label: "anders", category: "", icon: "pro-pic.jpg" },
{ label: "andreas", category: "", icon: "pro-pic.jpg" },
{ label: "antal", category: "", icon: "pro-pic.jpg" },
{ label: "annhhx10", category: "Products", icon: "pro-pic.jpg" },
{ label: "annk K12", category: "Products", icon: "pro-pic.jpg" },
{ label: "annttop C13", category: "Products", icon: "pro-pic.jpg" },
{ label: "anders andersson", category: "People", icon: "pro-pic.jpg" },
{ label: "andreas andersson", category: "People", icon: "pro-pic.jpg" },
{ label: "andreas johnson", category: "People", icon: "pro-pic.jpg" },
{ label: "Dicson Person", category: "People", icon: "pro-pic.jpg" }
];
jQuery( "#search-auto, #search-auto-sidebar" ).catcomplete({
delay: 0,
source: data
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment