I tried the above answers. However, one problem is that if the category is not ordered, e.g.
var availableTags = [
{category: "favourite", label: "c#", value: "c#", },
{category: "other", label: "Java", value: "Java"},
{category: "favourite", label: "JavaScript", value: "JavaScript"},
{category: "other", label: "J#", value: "J#"},
];
it will create duplicates "favourite" and "other" category.
Here is a working demo I created for jquery ui autocomplete grouping. This can categorize items even when their categories are not in sorted order.
http://jsfiddle.net/jooooice/qua87frd/
$(function(){
var availableTags = [
{category: "favourite", label: "c#", value: "c#", },
{category: "other", label: "c++", value: "c++"},
{category: "other", label: "c", value: "c"},
{category: "other", label: "Java", value: "Java"},
{category: "favourite", label: "JavaScript", value: "JavaScript"},
{category: "other", label: "J#", value: "J#"},
];
var customRenderMenu = function(ul, items){
var self = this;
var categoryArr = [];
function contain(item, array) {
var contains = false;
$.each(array, function (index, value) {
if (item == value) {
contains = true;
return false;
}
});
return contains;
}
$.each(items, function (index, item) {
if (! contain(item.category, categoryArr)) {
categoryArr.push(item.category);
}
console.log(categoryArr);
});
$.each(categoryArr, function (index, category) {
ul.append("<li class='ui-autocomplete-group'>" + category + "</li>");
$.each(items, function (index, item) {
if (item.category == category) {
self._renderItemData(ul, item);
}
});
});
};
$("#tags").tagit({
autocomplete: {
source: availableTags,
create: function () {
//access to jQuery Autocomplete widget differs depending
//on jQuery UI version - you can also try .data('autocomplete')
$(this).data('uiAutocomplete')._renderMenu = customRenderMenu;
}
}
})
});
.ui-autocomplete-group {
line-height: 30px;
background-color: #aaa;
}
.ui-menu-item {
padding-left: 10px;
}
<input id="tags" type="text" />
category
var to the returned results. Then natedavisolds's example will get you the rest of what you need. – Pyrometer