【问题标题】:Angular show components between table rows表格行之间的角度显示组件
【发布时间】:2021-08-26 03:14:41
【问题描述】:

我在组件内有一个表格视图,其中包含使用 *ngFor 指令呈现的行。当用户单击特定行时,我想在该行下方显示一个子组件。以下是我的代码。

<tbody>
<ng-container *ngFor="let item of items" >
<tr (click)="expand(item)">
  <td>{{item.name}}</td>
  <td>{{item.title}}</td>  
  <td>{{item.description}}</td>
</tr>
<tr *ngIf="isexpanded(item)">
  <app-item-comp [item]="item"></app-item-comp>
</tr>
</ng-container>

当我单击一行时,这将显示子组件 app-item-comp。但它的宽度与行的第一个元素相同,而不是占据整个窗口。我想查看这个具有整个表格宽度的子组件。我怎样才能做到这一点?谢谢。

【问题讨论】:

  • 如果app-item-comp'返回'一个tdth)元素,试试colspan attribute
  • 在我的子组件中,我有另一个表。

标签: html angular typescript


【解决方案1】:

// Consider below code is in you ts file

let items: any = [
    {
        itemName: 'ABC',
        itemId: '1',
        isSubRowDisplayed: false
    },
    {
        itemName: 'PQR',
        itemId: '2',
        isSubRowDisplayed: false
    },
    {
        itemName: 'MNO',
        itemId: '3',
        isSubRowDisplayed: false
    }
];

expand(clickedItem: any): any {
    clickedItem.isSubRowDisplayed = !clickedItem.isSubRowDisplayed;
}
<tbody>
<ng-container *ngFor="let item of items" >
<tr (click)="expand(item)">
  <td>{{item.name}}</td>
  <td>{{item.title}}</td>  
  <td>{{item.description}}</td>
</tr>
<tr *ngIf="item?.isSubRowDisplayed">
  <app-item-comp [item]="item"></app-item-comp>
</tr>
</ng-container>

【讨论】:

  • 我想要一种显示子组件的方法,它与主表具有相同的宽度。这并不能解决这个问题。
  • @NirmalGamage,我认为这可以通过使用两个不同的 CSS 类来实现。当 isSubRowDisplayed = false 时将应用一个 CSS 类,而当 isSubRowDisplayed = true 时将应用另一个 CSS 类。现在,当我们单击特定行时,我们必须为整个表格或行设置宽度。通过使用 [ngClass] 你可以实现它。
猜你喜欢
  • 2018-10-25
  • 1970-01-01
  • 2022-01-24
  • 2021-02-07
  • 1970-01-01
  • 2020-12-18
  • 1970-01-01
  • 2017-11-09
  • 1970-01-01
相关资源
最近更新 更多