0

enter code here我正在尝试在 facebook 中重现搜索建议。它显示在带有标题的不同“类型”的同一框中结果(人员,组,页面,..)

基本上我需要按类型对结果进行分组,并添加一个带有类型名称的不可cliclable标题(例如:用户、组);就像脸书一样:

在此处输入图像描述

这是来自 jquery ui 页面的自定义 catcomplete:

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

-编辑-

可以通过在 serch 字段中输入“bootcamp”在此处进行测试(无需登录):http://209.51.221.243/integracion/login.php(输入“deya”有效,输入“bootcamp”无效

当只有一种结果时,Wich 工作正常,但当有多个结果时,例如在此响应中:

[{"value":"Donde puedo descargar los drivers de bootcamp?","id":"1","category":"Ayudas"}][{"value":"Donde puedo descargar los drivers de bootcamp?","id":"1","category":"Ayudas"},{"first_name":"bootcamp","last_name":"bootcamp","id":"95","value":"bootcamp bootcamp","category":"Usuarios"}]

它没有显示任何结果,为什么?

4

2 回答 2

0

在您的 php 中,只为每种类型的第一行添加子。您可以使用全局变量来做到这一点:

$first = array();

while (...) {
   if (!$first[$row['type']]) {
       $first[$row['type']] = true;
       $row['sub'] = $row['type'];
   }
}

然后在你的javascript中:

.data( "autocomplete" )._renderItem = function( ul, item ) {
            var sub = "";
            if (item.sub) {
                sub = $("<li class="sub">" + item.sub + "</li>");
                ul.append(li);
            }
            var li = $( "<li></li>" )
                .data( "item.autocomplete", item )
                .append( "<a><img src='"+item.img+"' /><span class='name'>" + item.value + "</span></a>" )
            ul.append(li);
                        };
于 2012-06-03T22:14:55.003 回答
0

您可能想看看http://jquery-ui.googlecode.com/svn-history/r3820/trunk/demos/autocomplete/categories.html

$.extend($.ui.menu.prototype, {
    next: function() {
        this.move("next", ".ui-menu-item:first");
    },
    previous: function() {
        this.move("prev", ".ui-menu-item:last");
    },
    move: function(direction, edge) {
        if (!this.active) {
            this.activate(this.element.children(edge));
            return;
        }
        var next = this.active[direction + "All"]('.ui-menu-item').eq(0);
        if (next.length) {
            this.activate(next);
        } else {
            this.activate(this.element.children(edge));
        }
    }
});

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

$(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({
        source: data
    });
});

我几乎忘记了我不久前创建了一个非常相似的 jsFiddle 示例:jsFiddle example

于 2012-06-04T01:40:24.627 回答