【问题标题】:How to set Angular Material mat-table cell conditionally?如何有条件地设置 Angular Material mat-table 单元格?
【发布时间】:2021-01-09 04:01:25
【问题描述】:

我有以下垫表,每条记录都有编辑和删除按钮:

Name   |   Operation     |
-------|-----------------|
Bob    |   Edit | Delete |
John   |   Edit | Delete |
Willy  |   Edit | Delete |

我使用的mat-table结构如下图:

<ng-container matColumnDef="operation">
    <th mat-header-cell *matHeaderCellDef>Operation</th>
    <td mat-cell *matCellDef="let row">
      <ng-container *ngFor="let operation of operations">
        <button [disabled]="row.isActive">
          <mat-icon>{{ operation.icon }}</mat-icon>
        </button>
      </ng-container>
    </td>
</ng-container>

我想通过从父组件发送参数(“operation.isDisabled”)并在每一行中使用列值(“row.isActive”)来启用/禁用删除按钮。但是,由于它是基表,我不想使用特定值并尝试通过“操作”变量(如 operation.condition = "row.isActive && operation.type === 'delete '”。但不幸的是我无法通过以下配置做到这一点(我可以通过配置,没有问题。但我需要在if条件下进行):

this.operation = [
  {
    type: 'delete',
    condition: 'row.isActive && operation.type === "delete"'
  }
];  

那么,是否可以使用从父组件发送的这两个条件作为字符串等操作单个按钮?

【问题讨论】:

    标签: javascript angular typescript angular-material mat-table


    【解决方案1】:

    您可以使用回调来验证它。

    喜欢: &lt;button [disabled]="check(operation.type, row.isActive)"&gt;

    你会在 check() 回调中执行所需的评估吗?

    【讨论】:

    • 感谢朋友,投了赞成票。但是我需要一个通用的解决方案,通过从父组件传递条件。有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    相关资源
    最近更新 更多