enter code here
我试图在脸书中重现搜索建议。它在同一个框中显示不同“类型”的结果与标题(人物,组,页面......)
基本上我需要按类型对结果进行分组,并添加一个带有类型名称的非cliclable标头(例如:user,group);就像facebook一样:
这是来自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'不会工作
当只有一种类型的结果时,它正常工作,但是当有多个结果时,就像在这个响应中一样:
[{"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"}]
它没有显示任何结果,为什么?
答案 0 :(得分:0)
在你的php中只为每种类型的第一行添加sub。 您可以使用全局变量:
$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);
};
答案 1 :(得分: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 。