Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic angular nested ng-repeat filter, hide title if empty

I have a nested list with a filter , it has days and activities.

objects looks like :

days:[ 
{name:"monday", acts[{title:"Zumba",start:"10am",end:"11am"},{}..]}...
]

Filter works good but I'd like to hide the day header (group.title) when filter returns 0 activities for that day.

I found some answers like this : AngularJS - hide parent element if children loop is empty (filtered)

And tried to implement with no success.

This is my code :

view

<ion-view view-title="{{titulo}}">
  <ion-content>
      <div class="container-hearder-image" style="background-image: url('{{img}}');" >
    </div>
    <h4>{{titulo}}</h4>
    <p>Seleccione Actividades</p>
      <div ng-repeat="c in colors">
          <ion-checkbox ng-click="includeColour(c)" class="item-bcg waves-effect waves-block waves-light"/> {{c}}
       </div>
    <ion-list ng-repeat="group in detailService.selected.days" >
        <div class="item item-divider bar bar-header bar-calm" >{{group.title}}</div>
        <ion-list ng-repeat="item in group.acts | filter:colourFilter">
          <ion-item >
            <h3>{{item.title}}</h3>
            {{item.start}} - {{item.end}}
          </ion-item>
        </ion-list>      
    </ion-list> 
  </ion-content>
</ion-view>

controller

.controller('HorarioCtrl', function($scope, $stateParams,detailService) {


    $scope.colors = ['Zumba','Combat','Spinning','HIIT','Pilates','Yoga'];
    $selected = detailService.selected
    $scope.detailService=detailService;
    $scope.titulo = detailService.selected.title;
    $scope.img = detailService.selected.img;
    colourIncludes = [];
    $scope.colourIncludes = colourIncludes

    $scope.includeColour = function(colour) {
        var i = $.inArray(colour, colourIncludes);
        if (i > -1) {
            colourIncludes.splice(i, 1);
        } else {
            colourIncludes.push(colour);
        }
    }

    $scope.colourFilter = function(fruit) {
        if (colourIncludes.length > 0) {
            if ($.inArray(fruit.title, colourIncludes) < 0)
                return;
        }

        return fruit;
    }
});

Regards

like image 368
llermaly Avatar asked Aug 25 '26 01:08

llermaly


2 Answers

You can use alias expression which will then store the intermediate results of the repeater after the filters have been applied.

<div class="item item-divider bar bar-header bar-calm" ng-if="filteredElements.length>0">{{group.title}}</div>
<ion-list ng-repeat="item in group.acts | filter:colourFilter as filteredElements">
 <ion-item >
   <h3>{{item.title}}</h3>
    {{item.start}} - {{item.end}}
  </ion-item>
</ion-list>  
like image 196
Manu Obre Avatar answered Aug 27 '26 15:08

Manu Obre


As the other post you linked above answered, ngShow would work in this scenario.

<h3 ng-show="item.title != null">{{item.title}}</h3>
like image 44
Thomas Juranek Avatar answered Aug 27 '26 15:08

Thomas Juranek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!