Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

html - Displaying data from API in a table using Angular

I am trying to display data I am fetching from API in a certain way. I want to display fullName, balance, isActive, registered, state and country. I want to display data based on users. So, one user - one line. Can this be done? I displayed data, but it looks ugly because it displays users based on country. One country - one row and all users from that country are in a single cell. I will place the code I got bellow. This is link to the API: https://fww-demo.herokuapp.com/ I also need to be able to filter it for all columns and sort it in asc or desc order. Pagination is a bonus. Displaying is most important for me right now. I am not allowed to use any npm library, meaning I have to do it all manually.

This is my HTML, or should I say, template for users component

<table>
<thead>
    <th>Country</th>
    <th>State</th>
    <th>Full name</th>
    <th>Balance</th>
    <th>Is Active</th>
    <th>Registered</th>
</thead>
<tbody>
    <tr *ngFor="let user of users; let i = index;">
         <td >
           {{ user.country }}
        </td>
        <td >
            <p *ngFor="let state of user.state">
                {{ state.name }}
            </p>
        </td> 
         <td> 
            <div *ngFor="let state of user.state">
                <p *ngFor="let userName of state.users">
                    {{ userName.fullName }}
                </p>
            </div>
        </td> 
        <td>
            <div *ngFor="let state of user.state">
                <p *ngFor="let bal of state.users">
                    {{ bal.balance }}
                </p>
            </div>
        </td>
        <td>
            <div *ngFor="let status of user.state">
                <p *ngFor="let active of status.users">
                    {{ active.isActive }}
                </p>    
            </div>
        </td>
        <td>
            <div *ngFor="let register of user.state">
                <p *ngFor="let isReg of register.users">
                    {{ isReg.registered }}
                </p>
            </div>
        </td>
    </tr>
</tbody>

This is typescript for users component.

import { Component, OnInit } from '@angular/core';
import { User } from '../model/user';
import { UserService } from '../user.service';

@Component({
  selector: 'app-users',
  templateUrl: './users.component.html',
  styleUrls: ['./users.component.css']
})
export class UsersComponent implements OnInit {

  users: User[] = [];

  constructor(private userService: UserService) { }

  ngOnInit(): void {
    this.userService.getAll().subscribe(result => {
      this.users = result;
      console.log(this.users);
    });
  }

}

This is users service.

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class UserService {

  private urlGetUser: string = 'https://fww-demo.herokuapp.com/';
  
  constructor(public http: HttpClient) { }

  getAll() {
    return this.http.get<any>(this.urlGetUser);
  }
}
question from:https://stackoverflow.com/questions/65901173/displaying-data-from-api-in-a-table-using-angular

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...