简体   繁体   English

ngFor Angular6的表行跨度

[英]Table rowspan with ngFor Angular6

This is a working snippet 这是一个工作片段

I want the table to be like this: 我希望桌子是这样的:

 <table class="table table-bordered "> <thead> <tr id="first"> <th id="table-header"> <font color="white">No.</font> </th> <th id="table-header"> <font color="white">Responsible team </font> </th> <th id="table-header"> <font color="white">Task name</font> </th> <th id="table-header"> <font color="white">Task description </font> </th> </tr> </thead> <tbody> <!-- 1 --> <tr id="second"> <td rowspan="3" id="rowspan">1</td> <td rowspan="3" id="rowspan">Team1</td> <td>Description1</td> <td id="text">Application1</td> </tr> <tr id="second"> <td>Description2</td> <td id="text">Application2</td> </tr> <tr id="second"> <td>Description3</td> <td id="text">Application3</td> </tr> <tr> <tr id="third"> <td rowspan="3" id="rowspan">2</td> <td rowspan="3" id="rowspan">Team2</td> <td>Description1</td> <td id="text">Application1</td> </tr> <tr id="third"> <td>Description2</td> <td id="text">Application2</td> </tr> <tr id="third"> <td>Description3</td> <td id="text">Application3</td> </tr> <tr> </tbody> </table> 

But I don't want to hardcode it, I want to use *ngFor 但我不想对其进行硬编码,我想使用*ngFor

This is how I've tried: 这是我尝试过的方法:

<table class="table table-bordered ">
    <thead>
        <tr id="first">
            <th id="table-header">
                <font color="white">No.</font>
            </th>
            <th id="table-header">
                <font color="white">Responsible team </font>
            </th>
            <th id="table-header">
                <font color="white">Task name</font>
            </th>
            <th id="table-header">
                <font color="white">Task description </font>
            </th>
        </tr>
    </thead>
    <tbody>
        <ng-container *ngFor="let i of finalL ; let index = index">
            <tr id="second">
                <td id="rowspan">{{index + 1 }}</td>
                <ng-container *ngFor="let j of i">
                    <td id="rowspan">{{j}}</td>
                </ng-container>
            </tr>
        </ng-container>
    </tbody>
</table>

But it is not working properly (you can see the difference on stackblitz link). 但是它不能正常工作(您可以在stackblitz链接上看到区别)。 How can I modify the code in order to obtain what I want? 如何修改代码以获得我想要的东西? Thank you! 谢谢!

I can achieve this by treating the data like this: 我可以这样处理数据:

In component class : 在组件类中

finalL = [
  ["Team1", "Description1", "Application1"],
  ["Team1", "Description2", "Application2"],
  ["Team1", "Description3", "Application3"],
  ["Team2", "Description1", "Application1"],
  ["Team2", "Description2", "Application2"],
  ["Team2", "Description3", "Application3"],
];

data() {
  // First find distinct teams and then filter information about him
  return this.finalL.map(x => x[0])
    .filter((v, i, a) => a.indexOf(v) === i)
    .map(x => ({ 
      name: x, 
      data: this.finalL.filter(y => y[0] === x)
    }));
}

and then on component template : 然后在组件模板上

<table class="table table-bordered ">
  <thead>
    <tr id="first">
      <th id="table-header">
        <font color="white">No.</font>
      </th>
      <th id="table-header">
        <font color="white">Responsible team </font>
      </th>
      <th id="table-header">
        <font color="white">Task name</font>
      </th>
      <th id="table-header">
        <font color="white">Task description </font>
      </th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let i of data(); let index = index">
      <td>{{ index + 1 }}</td>
      <td>{{ i.name }}</td>
      <td>
        <tr *ngFor="let j of i.data">
          <td>{{ j[1] }}</td>
          <td>{{ j[2] }}</td>
        </tr>
      </td>
    </tr>
  </tbody>
</table>

Print of the result table 打印结果表

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

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