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

时间:2012-05-09 15:28:57

标签: jquery jquery-ui autocomplete jquery-ui-autocomplete

我正在根据文档中的类别示例实现jQuery UI Autocomplete。我想将结果数添加到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>

2 个答案:

答案 0 :(得分:3)

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

{ "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/

答案 1 :(得分:0)

$.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);
                });
            }
        });