3

Here's a sample JSON code:

 $scope.info = [{"name":"Category1", "data":[{"name":"Item1"}, {"name":"Item2"}]}, {"name":"Category2", "data":[{"name":"Item3"}, {"name":"Item4"}]}]; 

I'm putting it in a list thanks to ng-repeat and I filter with a search box, I also order the results by categories :

<div ng-repeat="Cat in info"> <h3>{{Cat.name}}</h3> <ul> <li ng-repeat="Item in Cat.data | filter:search" > {{Item.name}} </li> </ul> </div> 

The problem is: when I search, for example "Item3", it shows the Item3 in Category2 but there's still "Category1" even if there's nothing below because categories are not filtered, only their content is.

So how can I say to AngularJS: "If category1's filtered content is empty, do not show it" ?

1 Answer 1

3

Assign the output of the filter to a variable, and then hide the header based on the length of that.

<div ng-repeat="Cat in info" ng-hide="filtered.length == 0"> <h3>{{Cat.name}}</h3> <ul> <li ng-repeat="Item in filtered = (Cat.data | filter:search)" > {{Item.name}} </li> </ul> </div> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.