Skip to content

Commit fb92086

Browse files
author
Tomas Kirda
committed
Implement grouping. Resolve #83
1 parent d90cc5e commit fb92086

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

content/styles.css

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
.autocomplete-suggestion { padding: 2px 5px; white-space: nowrap; overflow: hidden; }
66
.autocomplete-no-suggestion { padding: 2px 5px;}
77
.autocomplete-selected { background: #F0F0F0; }
8-
.autocomplete-suggestions strong { font-weight: normal; color: #3399FF; }
8+
.autocomplete-suggestions strong { font-weight: bold; color: #000; }
9+
.autocomplete-group { padding: 2px 5px; }
10+
.autocomplete-group strong { font-weight: bold; font-size: 16px; color: #000; display: block; border-bottom: 1px solid #000; }
911

1012
input { font-size: 28px; padding: 10px; border: 1px solid #CCC; display: block; margin: 20px 0; }

index.htm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ <h2>Ajax Lookup</h2>
1616
</div>
1717
<div id="selction-ajax"></div>
1818

19-
<h2>Local Lookup</h2>
20-
<p>Type country name in english:</p>
19+
<h2>Local Lookup and Grouping</h2>
20+
<p>Type NHL or NBA team name:</p>
2121
<div>
2222
<input type="text" name="country" id="autocomplete"/>
2323
</div>

readme.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ The standard jquery.autocomplete.js file is around 2.7KB when minified via Closu
5151
Set this option to force auto positioning in other cases.
5252
* `orientation`: Default `bottom`. Vertical orientation of the displayed suggestions, available values are `auto`, `top`, `bottom`.
5353
If set to `auto`, the suggestions will be orientated it the way that place them closer to middle of the view port.
54+
* `groupBy`: property name of the suggestion `data` object, by which results should be grouped.
5455

5556
Autocomplete instance has following methods:
5657

@@ -108,6 +109,7 @@ Local lookup (no ajax):
108109
Generated HTML markup for suggestions is displayed bellow. You may style it any way you'd like.
109110

110111
<div class="autocomplete-suggestions">
112+
<div class="autocomplete-group"><strong>NHL</strong></div>
111113
<div class="autocomplete-suggestion autocomplete-selected">...</div>
112114
<div class="autocomplete-suggestion">...</div>
113115
<div class="autocomplete-suggestion">...</div>
@@ -119,6 +121,9 @@ Style sample:
119121
.autocomplete-suggestion { padding: 2px 5px; white-space: nowrap; overflow: hidden; }
120122
.autocomplete-selected { background: #F0F0F0; }
121123
.autocomplete-suggestions strong { font-weight: normal; color: #3399FF; }
124+
.autocomplete-group { padding: 2px 5px; }
125+
.autocomplete-group strong { display: block; border-bottom: 1px solid #000; }
126+
122127

123128
##Response Format
124129

@@ -159,6 +164,17 @@ you can supply the "paramName" and "transformResult" options:
159164
}
160165
})
161166

167+
## Grouping Results
168+
169+
Specify `groupBy` option of you data property if you wish results to be displayed in groups. For example, set `groupBy: 'category'` if your suggestion data format is:
170+
171+
[
172+
{ value: 'Chicago Blackhawks', data: { category: 'NHL' } },
173+
{ value: 'Chicago Bulls', data: { category: 'NBA' } }
174+
]
175+
176+
Results will be formatted into two groups **NHL** and **NBA**.
177+
162178
##Known Issues
163179

164180
If you use it with jQuery UI library it also has plugin named `autocomplete`. In this case you can use plugin alias `devbridgeAutocomplete`:

scripts/demo.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,22 @@ $(function () {
4646
}
4747
});
4848

49+
var nhlTeams = ['Anaheim Ducks', 'Atlanta Thrashers', 'Boston Bruins', 'Buffalo Sabres', 'Calgary Flames', 'Carolina Hurricanes', 'Chicago Blackhawks', 'Colorado Avalanche', 'Columbus Blue Jackets', 'Dallas Stars', 'Detroit Red Wings', 'Edmonton OIlers', 'Florida Panthers', 'Los Angeles Kings', 'Minnesota Wild', 'Montreal Canadiens', 'Nashville Predators', 'New Jersey Devils', 'New Rork Islanders', 'New York Rangers', 'Ottawa Senators', 'Philadelphia Flyers', 'Phoenix Coyotes', 'Pittsburgh Penguins', 'Saint Louis Blues', 'San Jose Sharks', 'Tampa Bay Lightning', 'Toronto Maple Leafs', 'Vancouver Canucks', 'Washington Capitals'];
50+
var nbaTeams = ['Atlanta Hawks', 'Boston Celtics', 'Charlotte Bobcats', 'Chicago Bulls', 'Cleveland Cavaliers', 'Dallas Mavericks', 'Denver Nuggets', 'Detroit Pistons', 'Golden State Warriors', 'Houston Rockets', 'Indiana Pacers', 'LA Clippers', 'LA Lakers', 'Memphis Grizzlies', 'Miami Heat', 'Milwaukee Bucks', 'Minnesota Timberwolves', 'New Jersey Nets', 'New Orleans Hornets', 'New York Knicks', 'Oklahoma City Thunder', 'Orlando Magic', 'Philadelphia Sixers', 'Phoenix Suns', 'Portland Trail Blazers', 'Sacramento Kings', 'San Antonio Spurs', 'Toronto Raptors', 'Utah Jazz', 'Washington Wizards'];
51+
var nhl = $.map(nhlTeams, function (team) { return { value: team, data: { category: 'NHL' }}; });
52+
var nba = $.map(nbaTeams, function (team) { return { value: team, data: { category: 'NBA' } }; });
53+
var teams = nhl.concat(nba);
54+
4955
// Initialize autocomplete with local lookup:
5056
$('#autocomplete').devbridgeAutocomplete({
51-
lookup: countriesArray,
52-
minChars: 0,
57+
lookup: teams,
58+
minChars: 1,
5359
onSelect: function (suggestion) {
54-
$('#selection').html('You selected: ' + suggestion.value + ', ' + suggestion.data);
60+
$('#selection').html('You selected: ' + suggestion.value + ', ' + suggestion.data.category);
5561
},
5662
showNoSuggestionNotice: true,
5763
noSuggestionNotice: 'Sorry, no matching results',
64+
groupBy: 'category'
5865
});
5966

6067
// Initialize autocomplete with custom appendTo:

src/jquery.autocomplete.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@
613613

614614
var that = this,
615615
options = that.options,
616+
groupBy = options.groupBy,
616617
formatResult = options.formatResult,
617618
value = that.getQuery(that.currentValue),
618619
className = that.classes.suggestion,
@@ -621,6 +622,18 @@
621622
noSuggestionsContainer = $(that.noSuggestionsContainer),
622623
beforeRender = options.beforeRender,
623624
html = '',
625+
category,
626+
formatGroup = function (suggestion, index) {
627+
var currentCategory = suggestion.data[groupBy];
628+
629+
if (category === currentCategory){
630+
return '';
631+
}
632+
633+
category = currentCategory;
634+
635+
return '<div class="autocomplete-group"><strong>' + category + '</strong></div>';
636+
},
624637
index;
625638

626639
if (options.triggerSelectOnValidInput) {
@@ -633,6 +646,10 @@
633646

634647
// Build suggestions inner HTML:
635648
$.each(that.suggestions, function (i, suggestion) {
649+
if (groupBy){
650+
html += formatGroup(suggestion, value, i);
651+
}
652+
636653
html += '<div class="' + className + '" data-index="' + i + '">' + formatResult(suggestion, value) + '</div>';
637654
});
638655

0 commit comments

Comments
 (0)