首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Angular 6中修复html表行跨度?

如何在Angular 6中修复html表行跨度?
EN

Stack Overflow用户
提问于 2019-11-10 11:44:23
回答 2查看 935关注 0票数 0

我正面临一个动态表行跨度的问题。我试过了,但它不能正常工作。

我有一个数组json。我用angular 6 for循环绑定了一个表。我想要显示学院,研究校园MC,研究校园PC和总行跨度标题。我尝试按照自己的意愿显示Study Campus MC、Study Campus PC和Total,但在列的右侧显示了一些不必要的行。

输出表:

我想要像这样的桌子:

stackblitz link

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-11-10 13:12:07

为了归档它,我建议您像这样更改您的json对象格式。

代码语言:javascript
复制
testJson = [
    {
        "shortName": "FHSS",
        "children": [
            {
                "physicallyApply": 1,
                "onlineApply": 0,
                "semesterName": "Summer",
                "semesterYear": 2020,
                "programName": "B.A. in English",
                "studyCampus": "Main Campus",
                "programTotal": 1
            },
            {
                "physicallyApply": 0,
                "onlineApply": 7,
                "semesterName": "Summer",
                "semesterYear": 2020,
                "programName": "B.A. in English",
                "studyCampus": "Permanent Campus",
                "programTotal": 7
            }
        ]
    },
    {
        "shortName": "FSIT",
        "children": [
            {
                "physicallyApply": 1,
                "onlineApply": 2,
                "semesterName": "Summer",
                "semesterYear": 2020,
                "programName": "B. Sc. in Multimedia and Creative Technology",
                "studyCampus": "Main Campus",
                "programTotal": 3
            }
        ]
    }
];

然后,您必须像这样更改您的HTML。

代码语言:javascript
复制
<table border="1">
    <thead>
      <tr>
        <th>Faculty</th>
        <th>Program</th>
        <th>Physically Apply</th>
        <th>Online Apply</th>
        <th>Program Total</th>
        <th>Study Campus MC</th>
        <th>Study Campus PC</th>
        <th>Total</th>
      </tr>
    </thead>
    <tbody>
      <ng-container *ngFor="let item of testJson; let i = index">

        <tr *ngFor="let child of item.children; let x = index;">
          <td >{{x == 0 ? item.shortName : null}}</td>
          <td>{{child.programName}}</td>
          <td>{{child.physicallyApply}}</td>
          <td>{{child.onlineApply}}</td>
          <td>{{child.programTotal}}</td>
          <td *ngIf="x==0 && i==0" [attr.rowspan]="i">{{totalMC}}</td>
          <td *ngIf="x==0 && i==0" [attr.rowspan]="i">{{totalPC}}</td>
          <td *ngIf="x==0 && i==0" [attr.rowspan]="i">{{totalMC + totalPC}}</td>
        </tr>
  </ng-container>
    </tbody>                
 </table>

在这里CHeck it:Stackblitz

票数 1
EN

Stack Overflow用户

发布于 2019-11-10 12:23:05

您多次打印totalMC, totalPC and totalMC + totalPC (由于循环)。你必须确保它们只出现一次,使用ngIf,如下所示-

代码语言:javascript
复制
<tr *ngFor="let item of testJson; let i = index">
    <td>{{item.shortName}}</td>
    <td>{{item.programName}}</td>
    <td>{{item.physicallyApply}}</td>
    <td>{{item.onlineApply}}</td>
    <td>{{item.programTotal}}</td>
    <td *ngIf="i==0" [attr.rowspan]="i">{{totalMC}}</td>
    <td *ngIf="i==0" [attr.rowspan]="i">{{totalPC}}</td>
    <td *ngIf="i==0" [attr.rowspan]="i">{{totalMC + totalPC}}</td>
  </tr>

Stackblitz link

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58785549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档