Skip to content

Commit 5aaae1d

Browse files
committed
Working categorized autocomplete demo.
1 parent 8c1f4da commit 5aaae1d

File tree

1 file changed

+64
-31
lines changed

1 file changed

+64
-31
lines changed

demos/autocomplete/categories.html

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,74 @@
1313

1414
</style>
1515
<script type="text/javascript">
16+
$.each({
17+
prevOf: "previousSibling",
18+
nextOf: "nextSibling"
19+
}, function( method, traversal ) {
20+
$.fn[ method ] = function( selector ) {
21+
return this.pushStack( this.map(function() {
22+
var ret = this[ traversal ];
23+
while ( ret && !$( ret ).is( selector ) ) {
24+
ret = ret[ traversal ];
25+
}
26+
return ret;
27+
}) );
28+
};
29+
});
30+
31+
$.extend( $.ui.menu.prototype, {
32+
next: function() {
33+
this.move("next", ".ui-menu-item:first");
34+
},
35+
36+
previous: function() {
37+
this.move("prev", ".ui-menu-item:last");
38+
},
39+
40+
move: function(direction, edge) {
41+
if (!this.active) {
42+
this.activate(this.element.children(edge));
43+
return;
44+
}
45+
var next = this.active[direction + "Of"]('.ui-menu-item');
46+
if (next.length) {
47+
this.activate(next);
48+
} else {
49+
this.activate(this.element.children(edge));
50+
}
51+
}
52+
});
53+
54+
$.widget("custom.catcomplete", $.ui.autocomplete, {
55+
_renderMenu: function( ul, items ) {
56+
var self = this,
57+
currentCategory = "";
58+
$.each( items, function( index, item ) {
59+
if ( item.category != currentCategory ) {
60+
ul.append( "<li class='category'>" + item.category + "</li>" );
61+
currentCategory = item.category;
62+
}
63+
self._renderItem( ul, item );
64+
});
65+
}
66+
});
67+
</script>
68+
<script type="text/javascript">
1669
$(function() {
1770
var data = [
18-
{
19-
items: ["anders", "andreas", "antal"]
20-
},
21-
{
22-
category: "Products",
23-
items: ["annhhx10", "annk K12", "annttop C13"]
24-
},
25-
{
26-
category: "People",
27-
items: ["anders andersson", "andreas andersson", "andreas johnson"]
28-
}
71+
{ label: "anders", category: "" },
72+
{ label: "andreas", category: "" },
73+
{ label: "antal", category: "" },
74+
{ label: "annhhx10", category: "Products" },
75+
{ label: "annk K12", category: "Products" },
76+
{ label: "annttop C13", category: "Products" },
77+
{ label: "anders andersson", category: "People" },
78+
{ label: "andreas andersson", category: "People" },
79+
{ label: "andreas johnson", category: "People" }
2980
];
3081

31-
$.widget("custom.catcomplete", $.ui.autocomplete, {
32-
_renderMenu: function( ul, items ) {
33-
var self = this;
34-
$.each( items, function( index, item ) {
35-
if ( item.category ) {
36-
ul.append( "<li class='category'>" + item.category + "</li>" );
37-
}
38-
$.each( item.items, function( index, item ) {
39-
self._renderItem( ul, {
40-
label: item
41-
});
42-
});
43-
});
44-
}
45-
});
46-
4782
$('#search').catcomplete({
48-
source: function(request, response) {
49-
response(data);
50-
}
83+
source: data
5184
});
5285
});
5386
</script>
@@ -61,7 +94,7 @@
6194

6295
<div class="demo-description">
6396
<p>
64-
A categorized search result. Currently just static data, will match anything you type.
97+
A categorized search result. Try typing "a" or "n".
6598
</p>
6699
</div><!-- End demo-description -->
67100

0 commit comments

Comments
 (0)