Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"]
},
like image 217
Barry Gallagher Avatar asked Feb 08 '15 19:02

Barry Gallagher


1 Answers

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

like image 142
tasseKATT Avatar answered Sep 30 '22 13:09

tasseKATT