Skip to content

Instantly share code, notes, and snippets.

@shaik2many
Created January 12, 2015 20:06
Show Gist options
  • Save shaik2many/7cb94819213f0453d36e to your computer and use it in GitHub Desktop.
Save shaik2many/7cb94819213f0453d36e to your computer and use it in GitHub Desktop.
jquery autocomplete category
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> http://jsfiddle.net/jensbits/bM7ck/3/ - jsFiddle demo by jensbits</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.6.2.js'></script>
<link rel="stylesheet" type="text/css" href="/css/normalize.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<style type='text/css'>
<style>
.ui-autocomplete-category {
font-weight: bold;
padding: .2em .4em;
margin: .8em 0 .2em;
line-height: 1.5;
}
</style>
</style>
<script type='text/javascript'>//<![CDATA[
$(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 + "</li>" );
currentCategory = item.category;
}
self._renderItem( ul, item );
});
}
});
$(function() {
var data = [
{ label: "anders", id: "1", category: "" },
{ label: "andreas", id: "2", category: "" },
{ label: "antal", id: "3", category: "" },
{ label: "annhhx10", id: "4", category: "Products" },
{ label: "annk K12", id: "5", category: "Products" },
{ label: "annttop C13", id: "6", category: "Products" },
{ label: "anders andersson", id: "7", category: "People" },
{ label: "andreas andersson", id: "8", category: "People" },
{ label: "andreas johnson", id: "9", category: "People" }
];
$( "#search" ).catcomplete({
delay: 0,
source: data,
select: function(event, ui) {
$('#searchval').val(ui.item.id);
}
});
});
});//]]>
</script>
</head>
<body>
<label for="search">Search: </label>
<input id="search" />
<input id="searchval" />
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment