4

Hey I have a JSON array that I'm calling from a service. I have some counters that I'd like to print out the length of the total array and length of some sub items. I'm able to print out the full length of the array but I also would like to print out the total number of items that have a progress "green", "red", and "yellow". Seeing that it's an array of objects, how would I go about to get the length of green, red, yellow?

JSON:

[
   {
     name: User1,
     progress: "green"
   },
   {
     name: User2,
     progress: "yellow"
   },
   {
     name: User3,
     progress: "green"
   },
    {
     name: User4,
     progress: "red"
   },
]

I'm storing the service called as so:

$scope.requestDetails = [];
$scope.requestDetails = data;

HTML:

<div class="total">{{requestDetails.length}}</div>
<div class="red"></div>
<div class="yellow"></div>
<div class="green"></div>

Out of curiosity, I tried to print {{requestDetails.progress.length}} but it comes up empty and printing {{requestDetails[0].progress.length}} prints out the number of letters of the first object's progress value.

1

3 Answers 3

4

Use the angular built in filter:

{{requestDetails.lenght}}

{{(requestDetails | filter: {progress: 'red'}).length}}

You can also use a custom function on the controller for custom logic:

Controller code:

  $scope.colorFilter = function(color) {
    return function (obj) {
      return obj.progress === color;
    };
  };

{{(requestDetails | filter: colorFilter('red')).length}}

Note: since Angular 1.3 filters must declare their dependencies explicitly to create a $watcher on them, otherwise those parameters will not be dirty checked within the $digest cycle, and the output will not be affected by any 2-way bindings.

Plunker: http://plnkr.co/edit/zgzSqeMVnCQbpDitrSI3?p=preview

Sign up to request clarification or add additional context in comments.

4 Comments

Nice! but this unfortunately spits out the 10 $digest() iterations reached...i'm still digging into it
if you can elaborate on how this array is populated and \ or changed, perhaps I can offer some further assistance
Of course! I'm looping thru this array using ng-repeat on a table element...I have 3 counters on top of the table that would display the # of total, green,red,and yellow records...I think using this filter coupled with the ng-repeat spits out the digest error
Hey Oleg, it's fixed now! in my controller where I was calling my service and storing the aforementioned data, I had two instances of the same "GET" call to get my data...I removed the duplicate and works like a charm...thank you
0

You can prepare your data in the controller, counting every element:

$scope.itemData = {};
for (var i = 0; i < $scope.requestDetails.length; ++i) {
  var detail = $scope.requestDetails[i];
  if ($scope.itemData[detail.progress] === undefined) {
    $scope.itemData[detail.progress] = 0;
  }
  $scope.itemData[detail.progress]++;
}

and use it in your template:

<div class="red">{{ itemData.red }}</div>
<div class="green">{{ itemData.green }}</div>

Comments

0

If I am understanding the question correctly, the goal is to display counts of each progress type from the object?

If this is the case, here is one possible solution.

HTML

<div>{{requestDetails.length}}</div>
<div class="red">{{(requestDetails | filter: {progress: 'red'}).length}}</div>
<div class="green">{{(requestDetails | filter: {progress: 'green'}).length}}</div>

This method leverages the built in angular filter. For more reading on filters and angularjs, here are the docs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.