Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force single result #97

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 11 additions & 2 deletions angucomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ angular.module('angucomplete', [] )

$scope.hideResults = function() {
$scope.hideTimer = $timeout(function() {
// if no object selected, on blur pick the top one from the list

if ($scope.selectedObject == null) {
$scope.selectResult($scope.results[0], true);
}

$scope.showDropdown = false;
}, $scope.pause);
};
Expand Down Expand Up @@ -184,13 +190,13 @@ angular.module('angucomplete', [] )
}
}

$scope.selectResult = function(result) {
$scope.selectResult = function(result, showDropdown) {
if ($scope.matchClass) {
result.title = result.title.toString().replace(/(<([^>]+)>)/ig, '');
}
$scope.searchStr = $scope.lastSearchTerm = result.title;
$scope.selectedObject = result;
$scope.showDropdown = false;
if (typeof showDropdown === 'undefined') { $scope.showDropdown = false; } // optionally keep autocomplete open
$scope.results = [];
//$scope.$apply();
}
Expand Down Expand Up @@ -218,6 +224,9 @@ angular.module('angucomplete', [] )
}

} else if (event.which == 13) {
if ($scope.currentIndex == -1 && $scope.results.length > 0) {
$scope.currentIndex = 0;
}
if ($scope.results && $scope.currentIndex >= 0 && $scope.currentIndex < $scope.results.length) {
$scope.selectResult($scope.results[$scope.currentIndex]);
$scope.$apply();
Expand Down