我正在根据文档中的类别示例实现 jQuery UI 自动完成。我想将结果数添加到类别标题中,因此它不显示“产品”,而是显示“产品(3)”。我知道需要从示例中修改 _renderMenu 函数,但我无法理解如何修改它。任何帮助我走上正确的道路将不胜感激。
这是来自 jQuery UI 自动完成类别演示的示例代码:
<script>
$.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 );
});
}
});
</script>
<script>
$(function() {
var data = [
{ label: "anders", category: "" },
{ label: "andreas", category: "" },
{ label: "antal", category: "" },
{ 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" }
];
$( "#search" ).catcomplete({
delay: 0,
source: data
});
});
</script>
<div class="demo">
<label for="search">Search: </label>
<input id="search" />
</div>