Loop through JSON array within an angular controller

I am currently attempting to iterate through a JSON array and display the values on the frontend of my application. I have provided my code, but I'm having trouble retrieving specific values (startDate, endDate) from within the array and displaying them correctly.

JSON

"reportTypeObligationTypes": [{
    "reportTypeId": null,
        "consolidationScopeId": 4009,
        "startDate": "2014-01-01",
        "endDate": "9999-12-31",
        "frequencyCode": "Q",
        "reportTypeLabel": null,
        "reportTypeCode": null,
        "consolidationScopeCode": "C",
        "frequencyLabel": "Quarterly"
}]

Angular controller

xxxApp.controller('reportListController', function ($scope, $http, $location, $routeParams) {


    var service_url = "/mdmservice/services/reportTypeEntities/" + $routeParams.id;
    $http.get(service_url).success(

    function (data, status, headers, config) {
        $scope.reportTypeEntities = data;
        angular.forEach($scope.reportTypeEntities, function (item) {
            console.log(item.reportTypeObligationTypes);
        })
    });

    $scope.gridOptions = {
        paginationPageSizes: [25, 50, 100, 200, 300, 400, 500, 1000],
        paginationPageSize: 25,
    };
    $scope.gridOptions.data = 'reportTypeEntities';
    $scope.gridOptions.enableFiltering = true;
    $scope.gridOptions.enableGridMenu = true;
    $scope.gridOptions.fastWatch = true;

    $scope.gridOptions.columnDefs = [{
        name: "entityName",
        width: "35%",
        cellTemplate: '<div style="margin-left: 5px;">' + '  <a href="#edit/{{row.entity.entityId}}">{{row.entity.entityName}}</a>' + '</div>'
    }, {
        name: "entityCode",
        width: "15%"
    }, {
        name: "codeType"
    }, {
        name: "Entity Type",
        field: "entityType.entityTypeName"
    }, {
        name: "reportingId"
    }, {
        name: "Country",
        field: "country.countryName"
    }, {
        name: "startDate",
        field: "reportTypeObligationTypes.startDate"
    }, {
        name: "endDate",
        field: "reportTypeObligationTypes.endDate"
    }, {
        name: "consolidationScopeCode",
        field: "reportTypeObligationTypes.consolidationScopeCode"
    }, {
        name: "frequencyLabel",
        field: "reportTypeObligationTypes.frequencyLabel"
    }, {
        name: "Delete",
        cellTemplate: '<div style="margin-left: 5px;">' + '  <a href="#delete/{{row.entity.entityId}}" class="glyphicon glyphicon-trash btn btn-danger"> Delete</a>' + '</div>',
        enableFiltering: false,
        enableSorting: false
    }];

})

HTML Page - Angular UI grid

<div ng-app="xxxApp" ng-controller="reportListController">

<p class="heading">Entities with Reporting Obligation</p>

<!-- Entities list data table using - 'ui-grid' module  -->
<div ui-grid="gridOptions" class="myGrid" ui-grid-pagination
    ui-grid-selection ui-grid-resize-columns ui-grid-move-columns
    ui-grid-cellNav ui-grid-exporter></div>

Answer №1

To set the reportTypeObligationTypes in your controller, use

$scope.reportTypeObligationTypes = [{JSON}];
This is an example of how you can utilize it:

<div ng-repeat="item in reportTypeObligationTypes">
  <div>{{item.startDate}}</div>
  <div>{{item.endDate}}</div>
</div>

Each JSON object in the array will be represented by 'item', and you can access its properties using item.(property).

UPDATE:TEST IT OUT:

$scope.gridOptions = {
  data: $scope.myData,
  columnDefs: [
    { name: 'field1', displayName: 'display field' },
    { name: 'field2', visible: false }
 ]
};

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

Tips for updating the content within an HTML tag with jQuery

I am looking to update the parameter value of an applet tag based on the selection from a dropdown menu. As I am new to jQuery, I would appreciate any guidance on how to achieve this using jQuery. This is my current applet code: <applet id="decisiontr ...

Struggling with your Dropdown Menu onclick functionality in Javascript? Let me lend

I am seeking assistance with implementing a Javascript onclick Dropdown Menu. The current issue I am facing is that when one menu is opened and another menu is clicked, the previous menu remains open. My desired solution is to have the previously open menu ...

Testing the units of a system ensures that the flow and data storage are reliable

Currently, I'm facing an interesting challenge when it comes to unit testing and the flux data stores. Because the data stores are singletons that are only instantiated once (upon module import), any changes made during unit tests remain persistent. ...

What is the best way to customize material components using styled components?

What is the best approach to override material components with styled components, considering that material-ui component classes typically have higher priority than styled-component classes? Is using the !important flag the most effective solution? <bu ...

Deciphering Complex JSON Strings with Java

I am facing a challenge with a complex String structure that resembles the following, "data":"[ { "id": "123456", "from": { "name": "ABC", "id": "123" }, "m ...

How come this PHP script is generating JSON results without any specific instruction to do so?

Having trouble with a post request to the Google QPX API. I'm trying to save the data in a PHP array but the curl_exec() line isn't working as expected, and the JSON is displaying on screen instead. Any ideas on what might be going wrong with cU ...

Utilizing useEffect to retrieve and display an empty array in a React component

I am currently developing a React application that leverages Fetch to retrieve data from an API using SQLite. Strangely, when I check the console, it only displays a length of 3 and Array[0]. This means I can't access data based on specific IDs like I ...

How to extract variables from JSON data using PHP

After spending a considerable amount of time scouring this website for straightforward assistance, I find myself struggling to grasp the examples provided. The complexity of the code samples here makes it challenging to translate them into my own projects. ...

An element featuring a background color is vertically aligned in the middle of its parent container

Struggling to achieve a seemingly simple task, but coming up short on finding a solution. The goal is to have a background-color that aligns vertically in the middle of the first and last images in a stack of images. It may sound more complicated than it a ...

Utilizing the fs module in Node.js

Hello friends! Currently I am faced with an issue while trying to import the fs module in nodejs. Initially, I utilized require to import it like so: const fs = require('fs'); Everything was functioning smoothly until recently when it suddenly ...

How do I submit my form data as a JSON object using AngularJS?

I'm currently in the process of developing my first Angular app and trying to incorporate form submission into JSON data. Below is the form I have created: <span ng-show="show"> <form name="inputForm" class="form-group" ng-controller="For ...

Navigating JSON/API data within a Vue.js component template: step-by-step guide

I've successfully implemented a code snippet that renders a list of songs here: https://jsfiddle.net/jeremypbeasley/guraav4r/8/ var apiURL = "https://ws.audioscrobbler.com/2.0/?method=user.gettoptracks&user=thisisheroic&period=7day&limit= ...

An effective method for converting a string into a two-dimensional array using JavaScript

One challenge I am facing is checking from a string if a specific fruit has the correct amount on a given date. I've managed to achieve this by converting the string into a 2D array and iterating over the columns. While this method works, I can't ...

Having trouble implementing a directive in an AngularJS 1.5 component

After upgrading to angular version 1.5.0, I realized that a directive I created could be improved using the new .component() notation. However, the require property no longer functions as expected. Here is a simplified example: angular.module('myA ...

What are some ways to direct users from one page to another without relying on server-side programming?

Is there a way to create a redirect page using jQuery or JavaScript? What is the process of writing client-side scripting code to redirect the browser from one page (page1) to another page (page n)? ...

Accessing a webpage solely by logging in prevents unauthorized access

My login page currently redirects to a page named gallery.html upon successful login. However, I have noticed that entering /gallery.html in the URL also directly accesses the secure page without logging in. Can anyone suggest an effective way to impleme ...

How to extract a nested array in JSON within another array

I need help with deserializing a JSON file into an object. { "InputFileFolder": "D:\\MontlyTransactions\\", "TransactionGroups": [ { "Name": "Income", "FilterString": "Type=@0 and Amount>@1 and (Description.Cont ...

Why does HttpClient in Angular 4 automatically assume that the request I am sending is in JSON format?

Currently, I am working with Angular 4's http client to communicate with a server that provides text data. To achieve this, I have implemented the following code snippet: this.http.get('assets/a.txt').map((res:Response) => res.text()).s ...

Link a timestamp to a datetime-local input using ng-model

My challenge is to display an <input type="datetime-local> field using the ng-model attribute to represent a timestamp: <input type="datetime-local" ng-model="object.value"> This is being achieved with: $scope.object.value = 1433109600000; ...

How can I determine the size of the custom options dropdown in Magento?

I'm facing a challenging issue that I can't seem to crack. It might be because I'm still learning the ropes and struggling with technical jargon. Please bear with me as I try to explain my problem. Here is a summary of what I'm trying ...