简体   繁体   English

Rowspan 无法与 ngFor 在 angular 10 中正常工作

[英]Rowspan is not working properly with ngFor in angular 10

I have a table populating the data from json, There I have to use Rowspan to map the data properly.我有一个从 json 填充数据的表,在那里我必须使用 Rowspan 来正确映射数据。 Only single value will be used for Rowspan. Rowspan 将只使用单个值。 When I am using rowspan with static table its fine, but when it comes under loop its not coming properly.当我使用带有静态表的 rowspan 时它很好,但是当它进入循环时它不能正常工作。 I have commented the static table code.我已经注释了静态表代码。 Here is the code below https://stackblitz.com/edit/angular-haubxs?file=src%2Fapp%2Fapp.component.ts这是下面的代码https://stackblitz.com/edit/angular-haubxs?file=src%2Fapp%2Fapp.component.ts

app.component.html应用程序组件.html

<hello name="{{ name }}"></hello>
<p>
  Start editing to see some magic happen :)
</p>
<table class="table table-bordered">
  <thead>
    <tr>
      <td colspan="4" class="text-center">
        Document Audit Trail
      </td>
    </tr>
    <tr>
      <th rowspan="3">Certificate Number</th>
      <th>Document Version</th>
      <th>Issued on</th>
      <th>Cancelled on</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let item of data">
      <td rowspan="3" style="vertical-align:middle;">AZ12Q289</td>
      <td>{{item.versionNo}}</td>
      <td>{{item.issuedOn}}</td>

    </tr>
  </tbody>
</table>
<!--<table class="table table-bordered">
    <thead>
      <tr>
        <td colspan="4" class="text-center">
          Document Audit Trail
        </td>
      </tr>
      <tr>
        <th rowspan="3">Certificate Number</th>
        <th>Document Version</th>
        <th>Issued on</th>
        <th>Cancelled on</th>
      </tr>
    </thead>
    <tbody>
      
      <tr>
        <td rowspan="3" style="vertical-align:middle;">AZ12Q289</td>
        <td>Version 1</td>
        <td>19/5/2021 09:28</td>
        <td>22/5/2021 15:28</td>
      </tr>
      <tr>
 
        <td>Version 2</td>
        <td>22/5/2021 15:30</td>
        <td>28/5/2021 19:12</td>
        
      </tr>
      <tr>

        <td>Version 3</td>
        <td>22/5/2021 15:30</td>
        <td>-</td>
        
      </tr>
      
    </tbody>
  </table> -->

app.component.ts app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  name = 'Angular';
  data = [
    {
      versionNo: '1',
      issuedOn: '07/Jul/2021 11:15',
      
    },
    {
      versionNo: '2',
      issuedOn: '07/Jul/2021 11:15',
     
    }
  ];
}

This is because you're adding the <td> with rowspan in each <tr> , and not only in the first one.这是因为您要在每个<tr>添加带有 rowspan 的<td> <tr> ,而不仅仅是在第一个中。

So, the first one spans two rows in first column, so the next one will span two rows in second column (since first column is already taken), etc.所以,第一个跨越第一列的两行,所以下一个跨越第二列的两行(因为第一列已经被占用),等等。

So, you will want to have:所以,你会想要:

  • rowspan depending on number of rows in your data rowspan 取决于数据中的行数
  • have your <td> with rowspan occur only once, for the first item in your array.让带有 rowspan 的<td>只出现一次,对于数组中的第一项。

For that you can simply check index and length of the array, like so:为此,您可以简单地检查数组的索引和长度,如下所示:

<tbody>
    <tr *ngFor="let item of data; let index = index;">
      <td *ngIf="index === 0" [attr.rowspan]="data.length" style="vertical-align:middle;">AZ12Q289</td>
      <td>{{item.versionNo}}</td>
      <td>{{item.issuedOn}}</td>
    </tr>
  </tbody>

Here is forked stackblitz that works with dynamic number of rows.这是用于动态行数的分叉 stackblitz

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

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