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

What options are available to enable the user to input information into the v-time-picker component?

I am looking for a solution where users can input digits into the vuetify v-time-picker, while still being able to select the time on the clock. <v-col align-self="center"> <v-menu ref="menuTimeStart" v-model="me ...

`I'm having difficulty creating a dual axis chart with angular-google-chart`

I have been struggling to create a chart with two y-axis scales using angular-google-chart. Despite trying various online examples that work with google chart directly, I have not been successful. Below is a simple example of the code inside my controller: ...

Pressing the 'Enter' key within a textarea in a JQuery

This question seems fairly straightforward. I have a text area where hitting "enter" submits the content. Even though I reset the text to "Say something..." after submission, the cursor continues to blink. Is there a way to require the user to click on ...

Having trouble getting a new input box to be added when clicking with AngularJS?

I am facing an issue with adding dynamic form fields to the database using PHP. I have utilized angular for this purpose, but only the last form field is getting inserted into the database. To address this, I attempted using arrays and loops to increment a ...

JSP form continues to submit despite JavaScript validation returning false

On my JSP page, I have a form named "deleteCont" and a JavaScript function called "validateDelete". When the user clicks the "Delete contact" button, the "validateDelete" function is triggered successfully, showing an alert message. Unfortunately, even whe ...

What steps can be taken to solve the JavaScript error provided below?

My objective is to create a new variable called theRightSide that points to the right side div. var theRightSide = document.getElementById("rightSide"); Once all the images are added to the leftSide div, I need to use cloneNode(true) to copy the left ...

Verify the dimensions of the file being uploaded

I have a file uploader component that requires a dimensions validator to be added. Below is the code for the validator: export const filesDimensionValidator = (maxWidth: number, maxHeight: number): ValidatorFn => (control: AbstractControl): Vali ...

Locate the class ID and refine the search results by another class

I am currently working on a task that involves extracting the first <li> element's id where it has the class name my_class, and checking if the <span> with the class Time contains a ":" using jquery. Below is the sample HTML structure: & ...

What are the steps to customizing the button bar in the uib-datepicker-popup?

I want to customize the button bar for uib-datepicker-popup. Currently, it is using .btn-info, .btn-danger, and .btn-success for the 'today', 'clear', and 'close' buttons. Is there a way to easily change these to different but ...

Discover the specific item within an array of objects

Anyone have information like this: const info = { Title : "Banana", Quantity : 10, Location : "Everywhere", Phone : 123456, A : 987, B : 654, } and there is another array of details as: const dataArr = ["Title",&q ...

Plugin for controlling volume with a reverse slider functionality

I have been customizing the range slider plugin found at to work vertically instead of horizontally for a volume control. I have successfully arranged it to position the fill and handle in the correct reverse order. For instance, if the value is set to 7 ...

The code "Grunt server" was not recognized as a valid command in the

I recently set up Grunt in my project directory with the following command: npm install grunt However, when I tried to run Grunt server in my project directory, it returned a "command not found" error. Raj$ grunt server -bash: grunt: command not found ...

Does anyone have any sample code on processing JSON data from a URL using Modified JavaScript Value in Java?

Could anyone provide some examples on how to handle returned Json data using Modified JavaScript Value? Here is the specific data I require from the json url: { "result": { "data": [ { "name": "page1", "period": "dia", "values": [ { "value": 4, "end_time" ...

Utilizing React JS to neatly display Firebase data in a table

Before I post, I always make sure to do my due diligence by searching on Google, YouTube, forums, or trying to figure it out on my own. I even check questions similar to mine asked by other people, but unfortunately, I'm stuck. Currently, I am using ...

Sorting price and date with Vue in Laravel - A beginner's guide

Attempting to sort by price and by date. See the code below. I decided not to use the controller for this sorting. Is it feasible to implement it here? It's somewhat functional, but there are some issues. When selecting price, it doesn't seem to ...

I encountered an infinite loop issue with Material UI tabs when loading my component within the navigation bar (AppBar > TabPanel)

I am currently working on an application for a school project, and I'm incorporating Material UI into it. Following the official documentation provided on their website. This is the link I have been referring to: Code. My goal is to load a new comp ...

Guide on accessing a web service using a PHP POST request

Today, I encountered a challenge where I need to access a web service that contains JSON data. In order to do so, I was instructed to utilize the PHP POST method to log into the web service. The instructions included an array with 3 key-value pairs. { " ...

How can JSON data size be minimized effectively?

We have encountered an issue with high throughput on some of our data stores. Our current approach involves serializing POJOs to JSON using Jackson, but we are looking for alternative methods to compress the JSON data. Initially, we considered using BSON ...

Trigger a pop-up alert box when the jQuery event $(document).ready is fired within a Smarty template

I'm currently attempting to make a popup message display when the document is fully loaded. Although I have successfully integrated Google Maps on another page, this task seems to be more challenging. Below is the code snippet: <html> < ...

Converting text data into JSON format using JavaScript

When working with my application, I am loading text data from a text file: The contents of this txt file are as follows: console.log(myData): ### Comment 1 ## Comment two dataone=1 datatwo=2 ## Comment N dataThree=3 I am looking to convert this data to ...