Barry Gallagher
Barry Gallagher

Reputation: 6246

angularjs ng-repeat filter based on array length

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"]
},

Upvotes: 5

Views: 6845

Answers (2)

tasseKATT
tasseKATT

Reputation: 38490

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

Upvotes: 11

RoyTheBoy
RoyTheBoy

Reputation: 658

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

Upvotes: 2

Related Questions