Skip to content

Instantly share code, notes, and snippets.

@pjlamb12
Last active October 12, 2015 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjlamb12/3fe98d18275437dad291 to your computer and use it in GitHub Desktop.
Save pjlamb12/3fe98d18275437dad291 to your computer and use it in GitHub Desktop.
ng-class Issue
<ul class="search-results" ng-show="searchKeyword != '' && inputIsFocused">
<li ng-repeat="provider in providerList | searchFilter:searchFilterType:searchKeyword" ng-class="{'highlighted': provider == arrowSelectedItem}" ng-click="setSearchKeyword(provider)">{{provider.name}}</li>
</ul>
$scope.arrowSelectedItem = null;
$scope.filteredProviderList = null;
function moveSelectedItem(direction) {
var index = $scope.filteredProviderList.indexOf($scope.arrowSelectedItem);
if( direction == 'up' && index <= 0 ) {
$scope.arrowSelectedItem = null;
} else if( direction == 'up' && index > 0 ) {
$scope.arrowSelectedItem = $scope.filteredProviderList[index-1];
} else if( direction == 'down' && index < $scope.filteredProviderList.length - 1 ) {
$scope.arrowSelectedItem = $scope.filteredProviderList[index+1];
}
}
var keyHandler = function(e) {
// If they hit enter while they've highlighted an item from the dropdown
if( e.keyCode === 13 && $scope.arrowSelectedItem != null ) {
$scope.setSearchKeyword($scope.arrowSelectedItem);
}
if( $scope.searchKeyword.length == 0 ) {
$scope.arrowSelectedItem = null;
}
if( $scope.inputIsFocused && $scope.searchKeyword != '' ) {
if( e.keyCode === 38 ) {
if( $scope.arrowSelectedItem != null ) {
moveSelectedItem('up');
}
} else if ( e.keyCode === 40 ) {
moveSelectedItem('down');
}
}
// If a letter key is pressed, filter the list so that our down arrow selection list is correct
if ( $scope.inputIsFocused && e.keyCode >= 48 && e.keyCode <= 90 ) {
$scope.filteredProviderList = searchFilterFilter($scope.providerList, $scope.searchFilterType, $scope.searchKeyword);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment