Skip to content
This repository has been archived by the owner on Nov 22, 2021. It is now read-only.

Commit

Permalink
feat(autocomplete): Add orderBy option
Browse files Browse the repository at this point in the history
Add a new option for sorting suggestions by one of their properties.
  • Loading branch information
Michal Krypczyk committed Sep 17, 2016
1 parent a4c03ca commit ee03dac
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/auto-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
* becomes empty. The $query variable will be passed to the expression as an empty string.
* @param {boolean=} [loadOnFocus=false] Flag indicating that the source option will be evaluated when the input element
* gains focus. The current input value is available as $query.
* @param {string=} [orderBy=NA] Property used to determine the order of matches.
* @param {boolean=} [selectFirstMatch=true] Flag indicating that the first match will be automatically selected once
* the suggestion list is shown.
* @param {expression=} [matchClass=NA] Expression to evaluate for each match in order to get the CSS classes to be used.
* The expression is provided with the current match as $match, its index as $index and its state as $selected. The result
* of the evaluation must be one of the values supported by the ngClass directive (either a string, an array or an object).
* See https://docs.angularjs.org/api/ng/directive/ngClass for more information.
*/
tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tagsInputConfig, tiUtil) {
tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, $filter,tagsInputConfig, tiUtil) {
function SuggestionList(loadFn, options, events) {
var self = {}, getDifference, lastPromise, getTagId;

Expand Down Expand Up @@ -83,6 +84,11 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tags

items = tiUtil.makeObjectArray(items.data || items, getTagId());
items = getDifference(items, tags);

if (options.orderBy) {
items = $filter('orderBy')(items, options.orderBy);
}

self.items = items.slice(0, options.maxResultsToShow);

if (self.items.length > 0) {
Expand Down Expand Up @@ -154,7 +160,8 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tags
loadOnEmpty: [Boolean, false],
loadOnFocus: [Boolean, false],
selectFirstMatch: [Boolean, true],
displayProperty: [String, '']
displayProperty: [String, ''],
orderBy: [String, null]
});

$scope.suggestionList = new SuggestionList($scope.source, $scope.options, $scope.events);
Expand Down
36 changes: 36 additions & 0 deletions test/auto-complete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,42 @@ describe('autoComplete directive', function() {
});
});

describe('order-by option', function() {
it('initializes the option to false', function() {
// Arrange/Act
compile();

// Assert
expect(isolateScope.options.orderBy).toBeNull();
});

it('sorts the suggestions by text in ascending order', function() {
// Arrange
compile('order-by="text"');

// Act
loadSuggestions(3);

// Assert
expect(getSuggestions().length).toBe(3);
expect(getSuggestionText(0)).toBe('Item1');
expect(getSuggestionText(2)).toBe('Item3');
});

it('sorts the suggestions by text in descending order', function() {
// Arrange
compile('order-by="-text"');

// Act
loadSuggestions(3);

// Assert
expect(getSuggestions().length).toBe(3);
expect(getSuggestionText(0)).toBe('Item3');
expect(getSuggestionText(2)).toBe('Item1');
});
});

describe('debounce-delay option', function() {
it('initializes the option to 100 milliseconds', function() {
// Arrange/Act
Expand Down

0 comments on commit ee03dac

Please sign in to comment.