简体   繁体   English

如何为表格的每一行创建展开/折叠功能? Angular6

[英]How can I create expand/collapse function for each row of table? Angular6

So I need to be able to expand some details on each row of a table. 因此,我需要能够在表的每一行上扩展一些细节。 Right now I'm having two issues: 现在我有两个问题:

  • Clicking the expand/collapse toggle will trigger the action for every row in the table. 单击展开/折叠切换将触发表中每一行的操作。
  • The first row always puts the details above it. 第一行始终将详细信息放在其上方。

Here's the code segment: 这是代码段:

<tbody>
  <tr *ngFor="let client of clients">
    <td class="details-control">
        <a class="btn btn-link" (click)="collapsed1=!collapsed1">
            <i class="material-icons">
              expand_more
            </i>
        </a>
    </td>
    <td>{{client.firstName}}</td>
    <td>{{client.lastName}}</td>
    <td>{{client.company}}</td>
    <td><input type="checkbox"></td>
  </tr>
  <div *ngIf="!collapsed1">
    <p>Details</p>
  </div>

</tbody>

And what it looks like: 看起来像什么:

Toggling 切换

Also I had my *ngFor statement in the tag earlier but I realized I can't hit individual client objects if I build a separate for details. 另外,我之前在标记中有* ngFor语句,但是我意识到如果为细节建立一个单独的客户端对象,则无法打中各个客户端对象。

Let me know how I can improve! 让我知道我可以如何改善!

It's a very common pattern. 这是非常常见的模式。 The best and quick solution is to use some ID instead of just a boolean in your collapsed1 variable. 最好,最快的解决方案是在collapsed1变量中使用一些ID而不是布尔值。

<tbody>
 <tr *ngFor="let client of clients">
  <td class="details-control">
    <a class="btn btn-link" (click)="collapsed1 = collapsed1 ? 0 : client.id ">
        <i class="material-icons">
          expand_more
        </i>
    </a>
</td>
<td>{{client.firstName}}</td>
<td>{{client.lastName}}</td>
<td>{{client.company}}</td>
<td><input type="checkbox"></td>
<div *ngIf="collapsed1=client.id">
  <p>Details</p>
</div>

You need a boolean array collapsed[] and use index in your ngFor, so you can use collapsed[i] . 您需要一个布尔数组collapsed[]并在ngFor中使用索引,因此可以使用collapsed[i] Take a look here for using index in ngFor: 看看这里在ngFor中使用index的方法:

ngFor using Index ng用于使用索引

Let me know if you need more info. 让我知道您是否需要更多信息。 Wellcome 惠康

Nevermind, here is the code that solved it. 没关系,这是解决它的代码。

<tbody>
  <tr *ngFor="let client of clients; let i = index">
    <td class="details-control">
        <a class="btn btn-link" (click)="client.hideme=!client.hideme">
            <i class="material-icons" *ngIf="!client.hideme">
              expand_more
            </i>
            <i class="material-icons" *ngIf="client.hideme">
                expand_less
              </i>
        </a>
    </td>
    <td width="30%">{{client.firstName}}
      <tr *ngIf="client.hideme">
        <td>Hey, I'm a bunch of details!</td>
      </tr>
    </td>
    <td>{{client.lastName}}</td>
    <td>{{client.company}}
        <tr *ngIf="client.hideme">
            <td>More Issuer details</td>
        </tr>
    </td>
    <td><input type="checkbox"></td>
  </tr>
</tbody>

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

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