Exploring Angular.JS: How to Access Sub-Arrays and Parse Keys

Trying my hand at building something with Angular for the first time, I'm facing an issue with retrieving JSON data.

The data is fetched from a SQL database in JSON format and passed to a template using Angular route:

.when('/tasks/:TaskID',{
    templateUrl: 'template/task_data_template.html',
    controller:"showTaskData",
    controllerAs: 'STD'
})

The showTaskData function is defined as follows:

angular.module('moonDive').controller('showTaskData',function($http,$routeParams){

var store = this; 

store.tasks= [];

json_Url = 'api/tasks_data.php?TaskID=' + $routeParams.TaskID;


$http.get(json_Url).success(function(data){
    store.tasks = data; 
})});

The structure of my data is as follows:

This can be accessed from the HTML template by:

{{STD.tasks[1]}}

Which returns the data in "JSON" format:

{
    "ActionID": "1",
    "Taskref": "1",
    "Ast1_1": "",
    "Ast2_1": "Start EVA watch\nopen           hatch\nAssist CDR",
    "Ast3_1": "",
    "Ast1_2": "Egress cabin to LM porch\nReceive &      jetttison bag\nReceive ETB/LEC",
    "Ast2_2": "Deploy CDR PLSS antenna\nHand jettison      bag to CDR\nHand ETB/LEC to CDR",
    "Ast3_2": "",
    "Ast1_3": "Descend lander to top rung\nUnlock and deploy MESA\nLower ETB on LEC",
    "Ast2_3": "Tape recorder -off\nVerify voice signals level and uitlity floo [......]"
}

Now, aiming to create a table with two columns (ast1, ast2) and X number of rows for X tasks. Unsure where to start, I attempted the following:

<table class="bordered hoverable responsive-table">
<tbody>

<tr ng-repeat="boo in STD.tasks[1]">
<td style=" color: blue;" ng-if="$odd"> {{boo}}</td>
<td style="color:red" ng-if="$even"> {{boo}}</td>
</tr>
</tbody>
</table>

Unfortunately, the above code did not work as expected. It displays all the information in what appears to be a random order.

I wish to remove rows with "1"; typically, I would use ng-if="boo.NameOfTheRow", but in this scenario, I do not have access to the name. How can I delete unnecessary data and organize my data by Astr1 and 2 (for columns) and task 1 to X (for rows)?

Thank you!

PS: The desired output should resemble:

<table>

<thead>
    <td> task </td>
    <td> Astr 1 </td>
    <td> Astr 2 </td>
    <td> Astr 3 </td>
</thead>

<tbody> 
<tr>
    <td> 1</td>
    <td> {{STD.tasks[1].Ast1_1}} </td>
    <td> {{STD.tasks[1].Ast2_1}}</td>
    <td>{{STD.tasks[1].Ast3_1}}   </td>
</tr>
<tr>
    <td> 2</td>
    <td> {{STD.tasks[1].Ast1_2}} </td>
    <td> {{STD.tasks[1].Ast2_2}}</td>
    <td>{{STD.tasks[1].Ast3_2}}   </td>
</tr>

<tr>
    <td> 3</td>
    <td> {{STD.tasks[1].Ast1_3}} </td>
    <td> {{STD.tasks[1].Ast2_3}}</td>
    <td>{{STD.tasks[1].Ast3_3}}   </td>
</tr>

....
<tr>
    <td> 7</td>
    <td> {{STD.tasks[1].Ast1_7}} </td>
    <td> {{STD.tasks[1].Ast2_7}}</td>
    <td>{{STD.tasks[1].Ast3_7}}   </td>
</tr>

    </tbody></table>

The data should display as:

Answer №1

When dealing with an array of entries like STD.tasks, it is important to update your ng-repeat statement to ng-repeat="boo in STD.tasks".

Typically, you will receive JSON data in the following format:

[
  {"field":"value", "field2": "value"},
  {"field":"value", "field2": "value"},
  {"field":"value", "field2": "value"},
  ...
]

Ensure that ng-repeat iterates over each value in the array rather than just the first item. Currently, it may be repeating over each property within the object.

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

Retrieve information from a database in real-time using Ajax without needing to set a refresh interval

Currently, I have been working on jquery/ajax requests and have managed to create an ajax request that retrieves data from a database. However, the issue at hand is that I am continuously utilizing window.setInterval() to refresh this function every x seco ...

Attaching an event listener to elements with a specified Class name

Currently facing a challenge where I am trying to implement a function that captures click events on squares. The objective is to capture the click event on every button with the square class. import { Component, OnInit } from '@angular/core&apos ...

To avoid the browser context menu from appearing when interacting with a d3.js element, simply employ a different way of clicking

Currently, I have a d3.js element plotted in the form of a rectangle: // draw rectangle svg.selectAll(".rect").append("rect") .attr("y", 10) .attr("x", 10) .attr("height", 5) .attr("width", 5) .on("contextmenu", function (d, i) { // ...

The Next.js website displays a favicon in Chrome, but it does not appear in Brave browser

As I work on my debut next.js website, I am configuring the favicon in index.js like this: <Head> <title>Create Next App</title> <link rel="icon" href="/favicon.ico" /> </Head> Initially, all my source ...

powershellUsing the cURL command with a PHP script

I am having trouble sending JSON data from the command line with curl to a php script and properly receiving it on the server-side. Here is what I have attempted so far: Running the following command in the command line: curl -H "Content-Type: applicati ...

Encountering issues with integrating interactjs 1.7.2 into Angular 8 renderings

Currently facing challenges with importing interactive.js 1.7.2 in Angular 8. I attempted the following installation: npm install interactjs@next I tried various ways to import it, but none seemed to work: import * as interact from 'interactjs'; ...

Validate forms using jQuery with the power of ajax

Is there a way to effectively check for the existence of a username? I want to increment a variable called "form_error" if the username already exists. If "form_errors" is greater than 0, I need the code to stop and return false. However, when I use an Aj ...

The React.js search feature consistently retrieves identical content every time

I am currently working on a project to create a search engine using React.js, specifically to search for GIPHY gifs through their API. However, I have encountered an issue where the gifs displayed do not update when I type in new words after erasing the pr ...

Create independent SVG files using Meteor and iron-router

My goal is to use Meteor and Iron-Router to serve dynamic SVG files with templating capabilities. To start, I create a new route: @route 'svg', { path: '/svg/:name' data: -> { name: this.params.name } # sample data layoutT ...

Ways to display the page's content within a div container utilizing Jquery

I am attempting to display the content of a URL page (such as ) within a <div> using JQuery, but so far I have been unsuccessful. Here is an example of what I am trying to achieve: <div id="contUrl"> .. content of google.fr page </div> ...

Obtaining JSON data in an Angular service: A beginner's guide

My JSON file has the following structure: { "user": [ { "id": 0, "data": [ { "userName": "iheb", "useremail": "", "userPassword": "kkk" } ], "questionnaireListe": [ { ...

Issues with the functionality of the ZeroClipboard Angular plugin

"> I'm fairly new to Angular and currently experimenting with a module called ZeroClipboard. I've made adjustments to my application in order to incorporate the module and configured it as per the demonstration. var app = angular.module(&a ...

Incorporating JavaScript and Alpine.js to Monitor and Log Changes in Input Range Values

I have developed a basic app that logs the input range value to the console when the range input changes. Interestingly, it works flawlessly when I slide the slider left and right with my cursor. However, when I programmatically change the value using Ja ...

Issue with jQuery hide() function in Internet Explorer 9

I need help with creating a hidden div that becomes visible when a user clicks a link. The code I have works in FF / IE7 / IE8 but not in IE9, where the div is always visible without any content. Any advice is appreciated! <script> $(document).r ...

Autocomplete is failing to provide any results

I am experiencing an issue with the autocomplete script as it is not displaying any names under the input field. Below is the code snippet from my index file: <head> <html lang="en"> <meta charset="utf-8"> <meta na ...

The main.js file will be served by nodeJS using express

After developing a nodeJS server using express, I encountered an issue where the server was only delivering the index.html file and not the accompanying main.js file. Both files are located in the same directory. app.get('/', function (req, res) ...

Creating a seamless REST API using Node.js and the Tedious library: A comprehensive guide

I am facing an issue with retrieving SQL data in my Express API. The page is continuously loading and showing a blank screen, however I can see the SQL data response in the console. Here is my index.js code : var Connection = require("tedious"). ...

A cookie designed to store and retain the preferred CSS stylesheet choice

I'm looking to create a cookie that will store the preference for a specific CSS stylesheet. I currently have a function in place that allows me to switch between stylesheets: function swapStylesheet(sheet){ document.getElementById('pagestyl ...

Declaring a function within a conditional statement

I recently came across a code sample in the book You Don't Know JS: Scope & Closures that is puzzling to me. "Function declarations that appear inside of normal blocks typically hoist to the enclosing scope, rather than being conditional as this ...

The concept of undefined in JavaScript when an if condition is applied

In Node.js, there is a method used to handle API requests. An unusual behavior occurs when dealing with req.query.foo - even if it has a value defined, it becomes undefined when used in an if condition as shown below. Additionally, another req.query.foo ...