0

I'm trying to filter my results based on a css class and can't seem to think of the easiest/best way to do that.

Here's my plunker example

Essentially by clicking on a link, I would like my filtered results to only show items where the "in_red" class is used.

1 Answer 1

2

Instead of filtering by class, filter by the actual property of the object. After all the class is set based on the name property.

One solution would be to use a custom filter function:

ng-repeat="friend in friends | filter:filterFriends"

Where filterFriends would be something like:

function(friend) {
  if ($scope.filterOnRed === true) {
    return friend.name.substring(0,1) == 'J';
  } else {
    return friend.name.indexOf($scope.searchText) != -1;
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you - that's what I needed. I needed some more work on this because I was going after the index as well and since I can't get the index inside of the function (at least I couldn't figure that out) I had to add an index element to my "friend" object and then I was able to reference it. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.