【问题标题】:How to find relation between table header (th) and td in a row in HTML (angular 5)?如何在 HTML(角度 5)中连续查找表头(th)和 td 之间的关系?
【发布时间】:2019-05-06 22:02:32
【问题描述】:

我检查了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 背景。请建议我替代设计,以按照下图以表格格式显示我的对象。

【问题讨论】:

    标签: html angular angular5


    【解决方案1】:

    您可以如下更新您的 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>
    

    【讨论】:

    • 我只是举了一个例子。就我而言,主题也是动态的。在您的代码中给出“j == 0”,因为该主题将从索引0的resultDetails中获取,但生物主题不存在,因此它只是将生物标记放在化学列中
    • 在你的情况下,数据是不可预测的。所以我想不可能只使用 HTML 来实现。您必须在 compoment.ts 文件中编写一些代码才能知道有多少主题。
    • 是的,我已经对类型脚本文件进行了更改,以便可以轻松地在 html 中打印数据及其工作。谢谢☺
    【解决方案2】:

    抱歉,我无法附上链接,但下面是代码。如果有帮助,请告诉我:

     //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;
            }
    

    【讨论】:

    • 我无法对主题进行硬编码,在我的情况下主题是动态的。
    猜你喜欢
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多