Search code examples
javascripthtmlangularjsangularjs-ng-repeatangular-ng-if

Get number of matching ng-if of a ng-repeat


Let's say I have something like that :

<md-grid-tile class="gray" ng-repeat="carto in cartoList" ng-if="search(carto)">
         <md-button ng-click="changeSVG(carto.fileName)" aria-label="carto.displayName">
             <img src="style/images/thumbnails/{{carto.fileName}}.png" width="100%" height="100%" title="{{carto.fullDisplayName}}" style="max-height: 220px;"></img>
         </md-button>
         <md-grid-tile-footer><h3 align="center">{{carto.displayName}}</h3> </md-grid-tile-footer>
</md-grid-tile>

Is there a way to retrieve the number of displayed tiles ? Meaning the number of elements in the ng-repeat matching the ng-if :

<md-grid-tile class="gray" ng-repeat="carto in cartoList" ng-if="search(carto)">

As requested, the search function :

    $scope.search = function (carto) {
    if ($scope.sideMenu) {
        var searchRegex = new RegExp('global', 'i');

        if (carto.fullDisplayName.search(searchRegex) != -1)
            return true;
    }
    else if ($scope.sideSearch) {
        if ($scope.searched.IS.indexOf(carto.informationSystem) === -1 && carto.informationSystem)
            return false;
        if ($scope.searched.area.indexOf(carto.area) === -1 && carto.area)
            return false;
        if ($scope.searched.block.indexOf(carto.block) === -1 && carto.block)
            return false;
        if ($scope.searched.type.indexOf(carto.type) === -1 && carto.type)
            return false;
        if ($scope.searched.level.indexOf(carto.level) === -1 && carto.level)
            return false;

        if ($scope.searchInput) {
            var searchRegex = new RegExp($scope.searchInput, 'i');

            if (carto.fullDisplayName.search(searchRegex) === -1)
                return false;
        }
        if (!$scope.homeVisible) {
            $scope.homeVisible = true;
            window.history.pushState("newUrl", "CartoViewer", window.location.origin + window.location.pathname);
        }
        return true;
    }
    else
        return true;
};

Solution

  • Personally, I would filter cartoList before you send it to the view:

    $scope.cartoListFiltered = function() {
        return $scope.cartoList.filter($scope.search);
    };
    

    And in your view:

    <md-grid-tile class="gray" ng-repeat="carto in cartoListFiltered()">
             <md-button ng-click="changeSVG(carto.fileName)" aria-label="carto.displayName">
                 <img src="style/images/thumbnails/{{carto.fileName}}.png" width="100%" height="100%" title="{{carto.fullDisplayName}}" style="max-height: 220px;"></img>
             </md-button>
             <md-grid-tile-footer><h3 align="center">{{carto.displayName}}</h3> </md-grid-tile-footer>
    </md-grid-tile>
    

    This runs your $scope.search against each individual carto and includes only the carto which return true. Same as ng-if except the logic is executed at the controller (where it should be)