What is the ideal method for presenting JSON data retrieved from an API in a tabular format?

As a newcomer to Angular, I am working on extracting data from an API located at . How can I display this JSON data in a tabular format within my Angular application? Are there any adjustments needed for the data to be presented in a tabular form upon serving the angular application?

Below is the TypeScript file Component.ts:

import { Component } from "@angular/core";
import { coronaApi } from "../service/service";
import { HttpErrorResponse } from "@angular/common/http";

@Component({
    selector:'corona',
    templateUrl:'./component.html'
})

export class CoronaComponent{
    public result:Array<any>;

    constructor(public service:coronaApi){}

    ngOnInit(){
        this.service.getData().subscribe((posRes)=>{
            this.result=(posRes);
        },(errRes:HttpErrorResponse)=>{
            console.log(errRes)
        })
    }
}

And here is the component.html content:

<table border="2">
    <thead>
        <tr>
           <th colspan="11"> Travel History </th>    
        </tr>
        <tr>
            <th>_CN6CA</th>
            <th>Accuracy Location</th>
            <th>Address</th>
            <th>Datasource</th>
            <th>Latlong</th>
            <th>Mode of Travel</th>
            <th>Pid</th>
            <th>Place Name</th>
            <th>Time From</th>
            <th>Time to</th>
            <th>Type</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let x of result";*ngFor = " let y of x ">
            <td>{{y._cn6ca}}</td>
            <td>{{y.accuracylocation}}</td>
            <td>{{y.address}}</td>
            <td>{{y.datasource}}</td>
            <td>{{y.latlong}}</td>
            <td>{{y.modeoftravel}}</td>
            <td>{{y.pid}}</td>
            <td>{{y.placename}}</td>
            <td>{{y.timefrom}}</td>
            <td>{{y.timeto}}</td>
            <td>{{y.type}}</td>
        </tr>
    </tbody>
</table>

Answer №1

Hopefully, this solution will be effective. Remember to utilize the second loop when subscribing to the getData() function.

this.result = response.travel_history;

Below is the code for component.ts

import { Component } from "@angular/core";
import { coronaApi } from "../service/service";
import { HttpErrorResponse } from "@angular/common/http";

@Component({
selector:'corona',
templateUrl:'./component.html'
})
export class CoronaComponent{
public result:Array<any>;
constructor(public service:coronaApi){}
ngOnInit(){
this.service.getData().subscribe((posRes)=>{
this.result=posRes.travel_history;
},(errRes:HttpErrorResponse)=>{
console.log(errRes)
})
}
}

Here is the code for component.html

<tbody>
        <tr *ngFor="let y of result">
            <td>{{y._cn6ca}}</td>
            <td>{{y.accuracylocation}}</td>
            <td>{{y.address}}</td>
            <td>{{y.datasource}}</td>
            <td>{{y.latlong}}</td>
            <td>{{y.modeoftravel}}</td>
            <td>{{y.pid}}</td>
            <td>{{y.placename}}</td>
            <td>{{y.timefrom}}</td>
            <td>{{y.timeto}}</td>
            <td>{{y.type}}</td>

        </tr>
    </tbody>

Answer №2

When working on your ts file, make sure to include a boolean and then display the data after receiving a response. There is no need for a second loop; simply assign
this.result = response.travel_history;

ts

isDataLoaded: boolean = false;
public result;

ngOnInit(){
   this.service.getData().subscribe(response => {
   this.result = response.travel_history;
   this.isDataLoaded = true;
   }); 
 }

html

<tbody *ngIf="isDataLoaded">
    <tr *ngFor="let x of result">
        <td>{{x._cn6ca}}</td>
        <td>{{x.accuracylocation}}</td>
        <td>{{x.address}}</td>
        <td>{{x.datasource}}</td>
        <td>{{x.latlong}}</td>
        <td>{{x.modeoftravel}}</td>
        <td>{{x.pid}}</td>
        <td>{{x.placename}}</td>
        <td>{{x.timefrom}}</td>
        <td>{{x.timeto}}</td>
        <td>{{x.type}}</td>

    </tr>
</tbody>

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 is the process for uploading files using AngularFire on Firebase Storage?

Despite watching multiple videos and tutorials, I am encountering a 403 error while working with Angular 1. To solve the issue of ng-model not supporting files, I created an Angular directive named file-model: app.directive('fileModel',['$ ...

Managing conflicting versions of React in a component library created with Webpack and Storybook

My goal is to create a React component library on top of MUI using Storybook and TypeScript. Since Storybook is based on Webpack (which includes SASS files), I'm utilizing Webpack to build the bundle because TSC can't compile those files. Subsequ ...

Incorporate an interface into a Typescript class to enhance its functionality

Is there a way to extend a class with the properties of an interface in Typescript while keeping those properties uninitialized? My ultimate goal is to minimize code redundancy by centralizing property declarations within an interface rather than duplicati ...

Can an Observable be created that emits an array produced by awaiting asynchronous methods?

I've encountered an issue with the following method: get fileResults$(): Observable<File[]> { return this.ngRedux.select<ICommunicationState>('communication').pipe(map(newState => { const files: File[] = []; ...

What is the method for converting a JSON string into an [object][object] format using Angular 6?

Is there a way to transform the data shown below into the [[object][object]] structure using Angular 6? resultArray = [{Q_id: "5bbee2fbbb34b98be0464c73", opt_id: 111},{Q_id: "5bbee2fbbb34b98be0464c73", opt_id: 113}] ...

Configuring web.config for Angular Universal deployment

I am trying to deploy Angular 4 Universal on Azure Web, but I am encountering some issues with the web.config file. The server.js file is located in the dist folder, so I set the path in the web.config as "dist/server.js". However, when the server.js runs ...

Changing the Id in Angular 2 Routing: A Step-by-Step Guide

Currently, I am working on an Angular 2 project with routing implementation. In the app.routing.module.ts file: { path: '', component: CComponent }, { path: 'Ddata:id', component: DComponent, children: [ { path: &apo ...

Tips for sharing data between two components

In my project, I have a customized Shared Component which consists of an input search bar with a "continue" button. This Shared Component is being utilized within two other components - the buy component and sell component. The challenge I am encountering ...

Error in Angular 1.6 caused by binding issues

Just started using Angular and encountering this issue. Error: Uncaught TypeError: Cannot use 'in' operator to search for '$ctrl' in subpackage I have a parent component and a child component where I have defined a function in the par ...

Transfer information using JWT tokens that can be easily interpreted by Angular

I am working on an Angular4 application that utilizes JWT for authentication. Using the angular2-jwt project on the client side, I have successfully implemented JWT. Now, I want to add additional data (such as full name and email) to the token on the serve ...

Merge information from different $http requests into a loop using AngularJS

Struggling to merge and display data from multiple $http calls in an HTML table? The process involves initiating a primary $http call for obtaining URLs, followed by subsequent calls within a loop to retrieve row data. The challenge lies in consolidating t ...

Implementing Angular's ngMessage template into your project

Hi there! I have a large form and I've been exploring the ngMessages feature. I was thinking of incorporating this: <script type="text/ng-template" id="error-messages"> <div ng-message="required">This field is required</div> <div ...

Trouble viewing Three.js content in Index.html

My current project involves building a website using three.js with typescript. However, I am facing an issue where only the header from my index.html file is displayed when I try to load the website onto a local server. The main problem arises when I atte ...

Is it possible to embed an Angular application within a specific DOM element?

I am the proud owner of two web applications. One is a traditional JavaScript web app, while the other is built on Angular (Version 10). My goal is to load the Angular app inside a DIV container after the JavaScript app has finished load ...

Taking command of the UI from another module in Angular

I am in the process of creating a tutorial module using Angular for my regular angular module. This tutorial is designed to assist older users who may need some extra guidance on how to navigate the UI. My goal is to have the tutorial seamlessly integrated ...

Preventing driver closure during test suites in Appium/Webdriverio: a step-by-step guide

Currently, I am in the process of testing a react native application with a specific test suite and test cases. The test case files I am working with are: login.ts doActionAfterLogin_A.ts Test Suite: [login.ts, doActionAfterLogin_A.ts] Issue at Hand: W ...

Using the <template> syntax to apply ngclass or [class.classname]

I am familiar with the syntax for ngFor, ngIf, and ngSwitch using templates. Can someone provide guidance on how to utilize ngClass or [class.className] with template syntax in Angular 2? Can anyone explain how to implement classes using template syntax i ...

Setting up a webpack configuration for js using ng-annotate-loader and babel-loader

I've experimented with two methods for simultaneously using ng-annotate and babel loaders on my *.js files. { //successfully executed test: /\.js?$/, loader: 'ng-annotate!babel?presets[]=es2015' } { //encountered a problem ...

How to Load Sorting in AngularJs Kendo Grid on Initialization

I have a kendo grid called "GridProjects" (Please refer to the attached image for the HTML structure). During initialization, I retrieve the column set (name, field, etc.) and the list of sorted columns from my database using the gridHelperService. While ...

The importance of response headers in Angular

When I make a request from WP Rest Api in Angular, I encounter an issue that puzzles me. service.ts get(): Observable<any>{ return this.http.get<any>('mysite.com/posts?categories=4&per_page=2')); } app-component.ts ngOnInit ...