Tallying elements within a nested array using Angular

I'm just starting with Angular and I'm struggling to figure out how to count all the results from a filtered array. The issue is that the data I need to filter is nested within a second-level array. I've searched for solutions on Stack Overflow, but none of them have worked for me. Here's what I currently have:

app.controller('mainController', function ($scope, $http, $location, $filter) {

    var externalData1 = [];
    var externalData2 = [];
    var externalData3 = [];
    var externalData4 = [];

    $http.get('app/data.json').then(function (data) {
        externalData1 = data.data;
        externalData2 = data.data; // This will vary in different cases, but the structure of the arrays remains the same
        externalData3 = data.data;
        externalData4 = data.data;

        $scope.Headers = [
            {
                'headerTitle': 'Morning Taskforce',
                'headerLinkCode': 'TFC',
                'headerData': externalData1
            },
            {
                'headerTitle': 'Op Taskforce',
                'headerLinkCode': 'opr',
                'headerData': externalData2
            },
            {
                'headerTitle': 'SAM Taskforce',
                'headerLinkCode': 'sbgr',
                'headerData': externalData3
            },
            {
                'headerTitle': 'Undefined Section Taskforce',
                'headerLinkCode': 'ufts',
                'headerData': externalData4
            }
        ];

        $scope.pendingCount = $filter('pendingFilter')($scope.Headers.headerData, { autr: 'PENDING' }).length;
    });
});

The structure of the data stored in data.json looks like this:

[
  {
    "_id": "569fd9c251f51d316e12efbe",
    "actionTaken": "PENDING",
    "actionTakenCode": "P",
    "autr": false,
    "action": "CANCEL",
    "actionAvailCode": "A",
    "tp": "male",
    "cta": 45.558392,
    "doc": 25.395304,
    "offset": 68305.2342,
    "client": "George Oneil",
    "terminal": 9215.6905
  },
  {
    "_id": "569fd9c27e048c82ce0564a4",
    "actionTaken": "CANCELED",
    "actionTakenCode": "C",
    "autr": false,
    "action": "AUTHORIZE",
    "actionAvailCode": "A",
    "tp": "male",
    "cta": 87.114735,
    "doc": -142.965417,
    "offset": 827448.2097,
    "client": "Fischer Ballard",
    "terminal": 2654.5002
  }
]

In my view template, I display the information as follows:

<ul data-role="listview" ng-repeat="header in Headers">
    <li data-role="list-divider" data-theme="a"><h2>{{header.headerTitle}}</h2></li>
    <li>
        <table data-role="table" class="ui-grid-b ui-responsive table-stroke">
            <thead>
                <tr>
                    <th>
                        ACTION TAKEN
                    </th>
                    <th>
                        OWNER
                    </th>
                    <th>
                        TP
                    </th>
                    <th>
                        CT
                    </th>
                    <th>
                        DOCUMENT
                    </th>
                    <th>
                        MF47
                    </th>
                    <th>
                        CLIENT
                    </th>
                    <th>
                        BUREAUS
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="item in header.headerData | orderBy: '-amount'">
                    <td>
                        <span class="indicator action-{{item.actionTakenCode}}">{{item.actionTaken}}</span>
                    </td>
                    <td>
                        {{item.autr}}
                    </td>
                    <td>
                        {{item.tp}}
                    </td>
                    <td>
                        {{item.cta}}
                    </td>
                    <td>
                        {{item.doc}}
                    </td>
                    <td>
                        <b>{{item.offset}}</b>
                    </td>
                    <td>
                        {{item.client}}
                    </td>
                    <td>
                        {{item.terminal}}
                    </td>
               </tr>
           </tbody>
       </table>
   </li>
</ul>

Currently, this setup creates tables for each header and displays the rows of data from the sub level arrays. However, I'd like to add an additional field to show the total number of PENDING items across all tables, which I attempted to achieve using the pendingCount variable without success.

<div id="overview">PENDING OPERATORS = {{pendingCount}} </div>

Answer №1

In order to solve this issue, make sure to create a variable called pendingCount within the $scope. Alternatively, you can define a function named getPendingCount in the $scope and perform the necessary calculations within that function. Feel free to let me know if there is any confusion with the context so I can offer further assistance.

Furthermore, refer to the documentation for $filter as it requires the second parameter to be an array.

https://docs.angularjs.org/api/ng/filter/filter

To address the problem, iterate through the Headers array and apply the filter on each element while calculating the total quantity.

Alternatively, you could utilize two for loops to count the records that meet your specified criteria.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Running compiler-produced assembler code within the program

Recently, I came across an interesting presentation discussing inline ASM generation in Javascript optimization. The presentation can be found at the following link: . In another paper located here: https://sites.google.com/site/juliangamble/Home/Compiler ...

Refresh the Angular controller for the modal every time it is accessed

Separate controllers have been defined for the base page and modal on that page, each containing a form with values retrieved from local storage. The issue arises when the modal controller loads the value for the elements upon DOM load, showing the same va ...

Is the custom attribute event being triggered too soon?

My Unique Component Creation Journey I have meticulously crafted a custom component to enhance the navigation of my application. The core structure consists of an ul element, with each li item dynamically generated based on the contents of the router&apo ...

Utilizing jQuery to enable a div to follow the touchmove or mousemove events while also automatically

I am looking to create a basic function in my jQuery script. I want the pages to slide horizontally in response to the movement of a finger or cursor on the screen. While there are many plugins available for this purpose, I prefer a simple, customized solu ...

Animating a spherical object in 3D space using THREE.js

As a newcomer to THREE.js with limited physics knowledge, I am in the process of developing a top-view football game engine. However, I am currently facing challenges related to the ball's movement. When attempting to move the ball from side to side, ...

What is the best place to keep my JWT token secure?

Currently, I am working on an application using vue.js and vuex that authenticates to a JSON API server through JWT tokens. The challenge I'm facing is determining the best method for storing the JWT token securely. If I choose to store the token in ...

Can a nodeJS script be written to automate selecting options and filling out forms in HTML?

Greetings everyone, I am new here so please excuse me if my formatting is not up to par. Currently, I am tackling a project for an internship and after some extensive research, I am stumped on how to automate certain reports in the next phase. In summary ...

Deactivate tag Script using JQuery

Is there a way to dynamically remove two <script> tags from a page at the document ready event? Each tag is assigned its own unique id. I attempted to use the following code: $("#idPrimoScript").remove(); $("#idSecondoScript").remove(); However, t ...

Discovering patterns within a series of binary digits in Python

Currently, I am working on an exciting AI project utilizing Python. This project involves filling an array with 0's and 1's based on user input, then predicting the next input by analyzing the sequence of these digits. I am curious if there are e ...

One way to display GIFs sequentially without preloading them is by using JavaScript

On my website, I'm trying to display five animated gifs sequentially. I want gif2 to appear and start its animation only after gif1 has finished its animation. However, the code I'm using is giving me some unexpected behavior. While it works corr ...

Using the size_t operator, the values in an array are incremented

Examining the Code Below: #include <stdio.h> #include <stddef.h> void main () { int n[5] = {1,2,3,4,5}; printf("%s%13s\n","element","value"); for (size_t i = 0; i<5; ++i) { ...

Establish a connection to a designated route in Node.js

I have a specific path that I want to establish a socket connection to: http://localhost:3000/home/. Below is my index.js code: var express = require('express'); var app = express(); var http = require('http').Server(app); var path = r ...

Tips for maintaining a scrollable Material-UI Table with dynamic height?

I'm encountering an issue with the Material-UI Table component in terms of its size and scrollability. Summary: The Material-UI table only becomes scrollable when its parent has a fixed size. If I set the size to 100% to fit the parent, it overflows. ...

An issue has occurred: Cannot access the properties of an undefined object (specifically 'controls')

I encountered an error message stating "TypeError: Cannot read property 'controls' of undefined" in the code that I am not familiar with. My goal is to identify the source of this error. Below is the HTML code snippet from my webpage: <div ...

Stop the webpage from scrolling when clicking on a ui-grid field

Is there a way to prevent page scrolling when clicking on a row field in ui-grid? I'm working with a page that has ui-grid, and each row includes an anchor tag with a URL value linked and target="_blank" to open in a new tab like the example below: ...

``Maybe there is a way to fix the issue of jQuery not functioning properly

I am currently working on integrating jquery with Reactjs to add a class on click event. The functionality works fine when the page is refreshed, but it stops working if I navigate to the page after clicking on any menu item without refreshing. How can I ...

Creating a JavaScript and C# popup for deleting records in asp.net

I'm struggling a bit with understanding how server-side deletion works. I know how to use MessageBox, but it's not the best approach. I've been advised to use a popup on the server side. What I'm trying to achieve is that when you clic ...

Sort through the angular data using an array of identifiers

I am working with a json file that contains my data [{"id":"349","title":"event 1"},{"id":"350","title":"event 2"},{"id":"351","title":"event 3"}] Users have the ability to save events to local storage, which are then stored in an array $storage.myEven ...

The issues of cross-origin domains arise when utilizing iframes with src links originating from the server side

Encountering a cross-origin domain issue, receiving the following error while working with an iframe: candidateInterviewCtrl.js:39 Uncaught SecurityError: Blocked a frame with origin "http://localhost:9000" from accessing a frame with origin "http://local ...

What is the equivalent of Angular's "$emit" in the React universe?

When working with React, what is the equivalent method to $scope.$emit in Angular for notifying ancestors? ...