Linda
Linda

Reputation: 53

Get number of results after filtering ng-repeat with ng-if

I am implementing a search function in AngularJS and I want there to be a message "No results found" when there are no matches. Since I am filtering only some of the fields and not all of them, I wrote a separate filter function that is applied in ng-if to check if the entry matches the search or not. Now I need to count the resulting panels AFTER ng-repeat is finished, so I tried using a directive after-repeat and assign a value to a results variable in my SearchController from inside that directive, but it's not doing anything.

HTML:

<div ng-if="searchctrl.results<1">No matches.</div>
<uib-accordion close-others="true"> 
    <uib-accordion-group ng-if="searchfilter(lokal)" is-open="isopen" ng-repeat="lokal in lokale | orderBy:'name'" after-repeat>
        [...]
    </uib-accordion-group>
</uib-accordion>

app.js (with controllerAs for my SearchController):

var app = angular.module('ZoiglApp', ['ngAnimate', 'ui.bootstrap', 'ngRoute', 'ngTouch', 'leaflet-directive']);

app.config(function($routeProvider) {
    $routeProvider
    [...]
    .when('/search/:searchterm', {
        controller: 'SearchController',
        controllerAs:'searchctrl',
        templateUrl: 'views/search.html'
    })
    .otherwise({
        redirectTo: '/home'
    });
});

afterRepeat.js:

app.directive('afterRepeat', function() {
    return {
        restrict: 'A',
        link: function(scope, elem, attrs) {
            if(scope.$last) {
                scope.searchctrl.results = angular.element('.panel').length;
            }
        }
    };
});

edit: I'm still searching for a solution. I have answered the two suggestions below with my problems with them. It would be really nice if someone could try to find my error

Upvotes: 0

Views: 952

Answers (2)

Alexandre
Alexandre

Reputation: 588

Just filter your list in an other in your controller and use this new one in your view.

$scope.filteredLokale = $filter('filter')($scope.lokale, { param: "paramValue" });

After that, get count with "length" parameter.

    <uib-accordion close-others="true"> 
    <uib-accordion-group is-open="isopen" ng-repeat="lokal in filteredLokale | orderBy:'name'" >
        [...]
    </uib-accordion-group>
</uib-accordion>
{{ filteredLokale .length }}

Upvotes: 1

rob
rob

Reputation: 18513

You should create an angular filter for your searchFilter function instead of using ng-if. Then you can assign the filtered array to a variable called filteredLokale like this

<uib-accordion-group ng-repeat="lokal in lokale | orderBy:'name' | searchFilter as filteredLokale">
</uib-accordion-group>
Length: {{filteredLokale.length}}

Upvotes: 0

Related Questions