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

feat(autocomplete): Add orderBy option #729

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/auto-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@
* 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] The property to order the suggestions by. Prefix the property with '-' to change direction.
* E.g.: 'label' for ascending order or '-label' for descending order.
* @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 +85,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 +161,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 null', 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