7

我正在根据文档中的类别示例实现 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>
4

2 回答 2

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/

于 2012-05-10T01:46:46.413 回答
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);
                });
            }
        });
于 2012-05-10T09:32:17.323 回答