|
13 | 13 |
|
14 | 14 | </style> |
15 | 15 | <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"> |
16 | 69 | $(function() { |
17 | 70 | 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" } |
29 | 80 | ]; |
30 | 81 |
|
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 | | - |
47 | 82 | $('#search').catcomplete({ |
48 | | - source: function(request, response) { |
49 | | - response(data); |
50 | | - } |
| 83 | + source: data |
51 | 84 | }); |
52 | 85 | }); |
53 | 86 | </script> |
|
61 | 94 |
|
62 | 95 | <div class="demo-description"> |
63 | 96 | <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". |
65 | 98 | </p> |
66 | 99 | </div><!-- End demo-description --> |
67 | 100 |
|
|
0 commit comments