T. Ferreira T. Ferreira - 3 months ago 11
AngularJS Question

How to count the number of rows containing a certain value?

I'm using AngularJS and I have a table that I populate using

ng-repeat
. Check this short example:

http://jsfiddle.net/sso3ktz4/

How do I check how many rows I have with a certain value? For example, in the fiddle above, how do I check how many rows I have with the word "second"? It should return "3".

AngularJS answers are preferred, although I also have JQuery available.

Thanks!

Answer

updated controller code is below, where $scope.findRowCount is required function

var myApp = angular.module('myApp', []).controller('MyCtrl', MyCtrl);

function MyCtrl($scope) {
    $scope.items = [{
        name: 'first',
        examples: [{
            name: 'first 1'
        }, {
            name: 'first 2'
        }]
    }, {
        name: 'second',
        examples: [{
            name: 'second'
        }, {
            name: 'second'
        }]
    }];


    $scope.findRowCount=function(value){

    var count=0;
    angular.forEach($scope.items, function(item, i){
    if(item.name==value){

      count=count+1;


    }
    angular.forEach(item.examples, function(exp, j){

    if(exp.name==value){

      count=count+1;


    }

    })




    });
    console.log("count"+count);
    return count;
    }

    var result=$scope.findRowCount("second");
    console.log(result);
}

http://jsfiddle.net/p3g9vyud/