$(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" />
自動完成類別例子聽起來接近你所描述的:http://jqueryui.com/demos/autocomplete /#categories查看源代碼,瞭解他們如何設置示例。如果您正在從數據庫中提取數據,只需將'category' var添加到返回的結果中即可。然後natedavisolds的例子會讓你得到你需要的其餘部分。 –
啊。儘管看起來不像,但我一直在瀏覽演示,但顯然我錯過了那一個。謝謝! – rogerkk