5

I was wondering, how do I put a filter a ng-repeat that will only bring back items that have colours? I was hoping to have a checkbox above the grid entitled "Show ones with colours" that would filter the list based on the count of the colours array when it was selected, and display ALL when unselected.

{
   "_id": "54d13c3f3c25d5d8123a1d62",
   "name": "Barry",
   "colours": ["239, 101, 128"]
},
{
   "_id": "54d13sfg5d5d8hgf6gg",
   "name": "John",
   "colours": []
},
{
   "_id": "34d13sfg5d5d4tt6g",
   "name": "Andrew",
   "colours": []
},
{
   "_id": "44d165d5d4t77t6g",
   "name": "Gary",
   "colours": ["25, 234, 22", "5, 100, 255"]
},

2 Answers 2

11

The following would give everyone not having colours:

<div ng-repeat="item in items | filter: { colours: '!' }">

Negate it again and you get everyone having colours:

<div ng-repeat="item in items | filter: { colours: '!!' }">

Demo: http://plnkr.co/edit/oIl3ohe0TLMWcQlTPuY5?p=preview

Sign up to request clarification or add additional context in comments.

1 Comment

That's perfect, thank you. I wasn't aware of the '!' and '!!'
2

I think you an use a controller function to test for the presence of colours so in haml/coffeescript

%tr{"ng-repeat2 => "item in list | filter: hasColour(item)"}

and hasColour is a controller function

 $scope.hasColour=  (item) ->
  item.colours.length > 0

Untested I'm afraid

1 Comment

The template syntax would be filter: hasColour

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.