TheLimeTrees
TheLimeTrees

Reputation: 411

Displaying the count of the number of duplicate items from ng-repeat

I've been attempting to display the count of the number of duplicate results from using ng-repeat to no avail.

<label data-ng-repeat="x in projects | unique:'b' | orderBy:'b'" >
    <input
    id="x.b"
    type="checkbox"
    ng-click="toggleSelection(x.b)" 
    ng-checked="selectedRefs.indexOf(x.b) > -1" />
    {{x.b}} 
</label>

http://plnkr.co/edit/zhH07DSHcofTxjFiE1TG?p=preview

As seen in the Plunker, these checkboxes loop through all the 'refs' and then display a unique checkbox for each one. What I want to do is add a count of sorts to them to show me how many times a certain 'ref' appears next to the checkbox. A written example would be that:

123 appears twice so it would show.

123 (2)

456 appears twice so it would show.

456 (2)

654 appears thrice so it would show.

654 (3)

987 appears once so it shows as.

987 (1)

Suggestions would be much appreciated.

Upvotes: 0

Views: 650

Answers (1)

K K
K K

Reputation: 18099

You can create a function which will return the count from the controller to the view

$scope.getCount = function (i) {
    var iCount = iCount || 0;
    for (var j = 0; j < $scope.projects.length; j++) {
        if ($scope.projects[j].b == i) {
            iCount++;
        }
    }
    return iCount;
}

And in the view part, call it by passing the value you want to search: getCount(x.b)

<label data-ng-repeat="x in projects | unique:'b' | orderBy:'b'">
    <input id="x.b" type="checkbox" ng-click="toggleSelection(x.b)" ng-checked="selectedRefs.indexOf(x.b) > -1" />{{x.b}}({{getCount(x.b)}})
</label>

Demo: http://plnkr.co/edit/RZokiI3XeSMlZB4gi4bJ?p=info

Upvotes: 1

Related Questions