jQuery UI使用计数机制自动完成类别

luk*_*mcd 7 jquery jquery-ui autocomplete jquery-ui-autocomplete

我正在基于文档中的类别示例实现jQuery UI自动完成.我想将结果数添加到Categories标题中,因此它不显示"Products"而是显示"Products(3)".我知道_renderMenu函数需要从示例中修改,但我无法理解如何修改它.任何帮助我开始正确的道路将不胜感激.

以下是jQuery UI Autocomplete Categories演示的示例代码:

    <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>
Run Code Online (Sandbox Code Playgroud)

And*_*ker 3

一种好的策略是创建一个对象/哈希,将类别存储为键,并将与该类别匹配的项目数组存储为值。换句话说,您想要构建这样的东西:

{ "Products": ["annhhx10", "annk K12", /*etc*/],
  "People": ["anders andersson", "andreas andersson", /*etc*/]
}
Run Code Online (Sandbox Code Playgroud)

一旦构建了它,您就可以迭代它并输出每个类别及其值。这样做的好处是,要获取项目数,您只需获取与当前类别相对应的数组的长度即可。就像是:

$.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);
            });
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

示例: http: //jsfiddle.net/andrewwhitaker/vNtGd/