带计数机制的jQuery UI自动完成类别
我正在根据文档中的类别示例实现jQuery UI Autocomplete。 我想将结果数量添加到类别标题中,因此不显示“产品”,而是显示“产品(3)”。 我知道_renderMenu函数需要从示例中修改,但我无法理解如何修改它。 任何帮助让我走上正确的道路将不胜感激。
以下是来自jQuery UI Autocomplete Categories demo的示例代码:
<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>
一个好的策略是创建一个对象/散列,将类别存储为键以及与该类别匹配的项的数组作为值。 换句话说,你想建立这样的东西:
{ "Products": ["annhhx10", "annk K12", /*etc*/],
"People": ["anders andersson", "andreas andersson", /*etc*/]
}
一旦你建立了它,你可以迭代它并输出每个类别,然后输出它的值。 这样做的好处是你所要做的就是获取项目的数量是取对应于当前类别的数组的长度。 就像是:
$.widget("custom.catcomplete", $.ui.autocomplete, {
_renderMenu: function(ul, items) {
var self = this,
categories = { };
/* Build a dictionary/hash where keys are categories and values are
* arrays of items with that category
*/
$.each(items, function (index, item) {
if (!categories.hasOwnProperty(item.category)) {
categories[item.category] = [item];
} else {
categories[item.category].push(item);
}
});
/* Iterate over the hash we just built and display a category followed by its
* items.
*/
$.each(categories, function(category, items) {
if (category) {
ul.append("<li class='ui-autocomplete-category'>" + category + " (" + items.length + ")</li>");
}
$.each(items, function (index, item) {
self._renderItem(ul, item);
});
});
}
});
例如: http : //jsfiddle.net/andrewwhitaker/vNtGd/
$.widget("custom.catcomplete", $.ui.autocomplete, {
_renderMenu: function (ul, items) {
var self = this,
currentCategory = "", itemCount = 0, itemsLength = items.length - 1;
$.each(items, function (index, item) {
if (item.category != currentCategory) {
ul.find('.ui-autocomplete-category:last').text(function () { return $(this).text() + ' ' + itemCount });
ul.append("<li class='ui-autocomplete-category'>" + item.category + "</li>");
currentCategory = item.category;
itemCount = 1;
}
else {
itemCount++;
}
if (index === itemsLength) {
ul.find('.ui-autocomplete-category:last').text(function () { return $(this).text() + ' ' + itemCount });
}
self._renderItem(ul, item);
});
}
});
链接地址: http://www.djcxy.com/p/10705.html
上一篇: jQuery UI Autocomplete categories with count mechanism
下一篇: Web.config transformation with SlowCheetah and ASP.NET Web site project