如何在HTML的一行中找到表头(th)和td之间的关系(角度5)?

时间:2018-12-05 10:47:58

标签: html angular angular5

我已经检查了SO post1SO post2,但这是通过jquery和javascrip完成的。但我想在html本身中做

我的对象结构就像

this.resultDetails = [
      new resultDetail(new Date('11/26/2018'), [
                 new scoreDetail('physics', '20'),
                 new scoreDetail('chemistry', '15')
                 ]),
      new resultDetail(new Date('10/12/2018'), [
                new scoreDetail('physics', '25'),
                new scoreDetail('chemistry', '20')
                ]),
      new resultDetail(new Date('7/1/2018'), [
                new scoreDetail('physics', '30'),
                new scoreDetail('Bio', '11')
                ])
    ];

我的html

<div>
<table thTable>
    <tr>
      <th>
        Analysis
      </th>
      <th *ngFor="let r of resultDetails">
        {{r.resultDate}}
      </th>
    </tr>
    <ng-container *ngFor="let r of resultDetails">
      <tr *ngFor="let s of r.scoreDetails">
          <td>{{s.subjectName}}</td>
          <td *ngIf="r.resultDate == ??">{{s.marks}}</td>
      </tr>
    </ng-container>
  </table>
</div>

在这里,我想检查日期并希望在相应的单元格中显示标记。我对HTML不那么熟悉,因为我来自WPF背景。请为我建议其他设计,以按照下面的图片以表格格式显示我的对象。

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以如下更新html代码。它将按您想要的方式工作。

<div>
<table thTable>
    <tr>
        <th>
            Analysis
        </th>
        <th *ngFor="let r of resultDetails">
            {{r.resultDate}}
        </th>
    </tr>

    <ng-container *ngFor="let r of resultDetails;let j = index">
        <tr *ngFor="let s of r.scoreDetails;let i = index">
            <ng-container *ngIf="j==0">
                <td>{{s.subjectName}}</td>
                <ng-container *ngFor="let r1 of resultDetails">
                    <td>{{r1.scoreDetails[i].marks}}</td>
                </ng-container>
            </ng-container>
        </tr>
    </ng-container>
</table>

答案 1 :(得分:0)

对不起,我无法附加链接,但是下面是代码。让我知道是否有帮助:

 //HTML 
 <div class="row">
          <table class="table" border="1">
            <thead>
              <tr>
                <th>Subject</th>
                <th *ngFor="let r of resultDetails">{{r.date | date:medium}}</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>Physics</td>
                <ng-container *ngFor="let r of resultDetails"> 
                <td *ngFor="let s of r.scoreDetail" [ngClass] = "{'prop' : s.name !== 'physics'}"> 
                  <span>{{s.marks}}</span>
                </td>
              </ng-container>
              </tr>
              <tr>
                <td>Chemistry</td>
                <ng-container *ngFor="let r of resultDetails"> 
                <td *ngFor="let s of r.scoreDetail" [ngClass] = "{'prop' : s.name !== 'chemistry'}"> 
                  <span>{{s.marks}}</span>
                </td>
              </ng-container>
              </tr>
            </tbody>
          </table>
        </div>

        //Typescript file
        import { Component, OnInit } from '@angular/core';
        import { resultDetail } from './result.details';
        import { scoreDetail } from './score.detail';

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


          resultDetails : resultDetail[] = [
            new resultDetail(new Date('11/26/2018'), [
                       new scoreDetail('physics', '20'),
                       new scoreDetail('chemistry', '15')
                       ]),
            new resultDetail(new Date('10/12/2018'), [
                      new scoreDetail('physics', '80'),
                      new scoreDetail('chemistry', '10')
                      ]),
            new resultDetail(new Date('7/1/2018'), [
                      new scoreDetail('physics', '30'),
                      new scoreDetail('chemistry', '2')
                      ])
          ];


          constructor() {
           }

          ngOnInit() {
          console.log("*****" +this.resultDetails);
          }

        }

        //model file
        import { scoreDetail } from "./score.detail";

        export class resultDetail {
            date: Date;
            scoreDetail: scoreDetail[];

            constructor(date : Date, scoreDetail : scoreDetail[]) {
                this.date  = date;
                this.scoreDetail = scoreDetail;
            }

        }

        //model file
        export class scoreDetail {
            name: string;
            marks: string;

            constructor(name : string, marks: string) {
                this.name = name;
                this.marks = marks;
            }
        }

        //css
        .prop {
            display: none;
        }