简体   繁体   English

如何在表格中显示嵌套 json 数组的数据

[英]How to display data in table for nested json array

This is my herolist json:这是我的英雄主义 json:

herolist = [{
    sl: 1,
    title: 'Batman',
    gender: 'male',
    firstname: 'Bruce',
    lastname: 'Wayne',
    city: 'Gotham',
    ticketprice: 123.4567,
    releasedate: '1/26/2018',
    poster: 'assets/images/batman.jpg',
    movieslist: [
        {
            title: 'Batman Begins',
            poster: 'assets/images/bat1_tn.jpg'
        }, {
            title: 'Dark Knight',
            poster: 'assets/images/bat2_tn.jpg'
        }, {
            title: 'Dark Knight Raises',
            poster: 'assets/images/bat3_tn.jpg'
        }
    ]

}

I have a nested array as movieslist.我有一个嵌套数组作为电影列表。 I need to display all those tiles inside movie list in the table.我需要在表格的电影列表中显示所有这些图块。

I followed the below approach to display remaining items我按照以下方法显示剩余项目

<h1>Heroes List Application</h1>
  <ul>
    <li *ngFor="let hero of herolist">{{hero.title}}</li>
  </ul>
  <div class="table-responsive">
  <table class="table table-striped table table-bordered table table-hover">
    <thead class="thead-dark">
      <tr>
      <th>Sl #</th>
        <th>Title</th>
        <th>Full Name</th>
        <th>Poster</th>
        <th>City</th>
        <th>Ticket Price</th>
        <th>Release Date</th>
        <th>Movies List</th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor="let hero of herolist">
        <td>{{hero.sl}}</td>
        <td>{{hero.title | titlecase }}</td>
        <td>{{hero.firstname+' '+hero.lastname}}</td>
        <td>
          <img width="50" [src]="hero.poster" [alt]="hero.title">
        </td>
        <td>{{hero.city}}</td>
        <td>{{hero.ticketprice | currency : 'INR': 'symbol': '3.2-3'}}</td>
        <td>{{hero.releasedate | date }}</td>
        **<td>
        <span>{{ hero.movieslist.values()}}</span>
      </td>**
      </tr>
    </tbody>
  </table>
  </div>
  `

I need to display the movies list in the column.我需要在列中显示电影列表。 How should I use the ngFor as it is not the taking movieslist.我应该如何使用 ngFor,因为它不是电影列表。

Use ngFor like this像这样使用 ngFor

<div *ngFor="let movie of hero.movieslist">{{ movie.title}}</div>

Full code完整代码

<h1>Heroes List Application</h1>
  <ul>
    <li *ngFor="let hero of herolist">{{hero.title}}</li>
  </ul>
  <div class="table-responsive">
  <table class="table table-striped table table-bordered table table-hover">
    <thead class="thead-dark">
      <tr>
      <th>Sl #</th>
        <th>Title</th>
        <th>Full Name</th>
        <th>Poster</th>
        <th>City</th>
        <th>Ticket Price</th>
        <th>Release Date</th>
        <th>Movies List</th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor="let hero of herolist">
        <td>{{hero.sl}}</td>
        <td>{{hero.title | titlecase }}</td>
        <td>{{hero.firstname+' '+hero.lastname}}</td>
        <td>
          <img width="50" [src]="hero.poster" [alt]="hero.title">
        </td>
        <td>{{hero.city}}</td>
        <td>{{hero.ticketprice | currency : 'INR': 'symbol': '3.2-3'}}</td>
        <td>{{hero.releasedate | date }}</td>
        **<td>
        <div *ngFor="let movie of hero.movieslist">{{ movie.title}}
          <img src={{movie.poster}}/>
        </div>
      </td>**
      </tr>
    </tbody>
  </table>
  </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM