【问题标题】:How to loop mat-cell object which contains array of objects in mat table如何循环包含mat表中对象数组的mat-cell对象
【发布时间】:2020-03-01 03:49:36
【问题描述】:

我已将包含对象数组的对象数组传递给 mat 表中的 dataSource。 我无法遍历具有对象数组的对象。

    const roleListData = [
  {
    roleId: 1,
    role: "admin",
    description: "Testing",
    level: "1",
    roleFunctions: [
      {
        createdBy: "person-1",
        createdOn: "2019-11-04T20:02:52.345",
        updatedBy: null,
        updatedOn: null,
        segmentId: 2,
        segmentType: "Dealer",
        segmentName: "testing",
        segmentValue: "5",
        additionalInfo: null,
        active: true
      }
    ]
}
]

roleFunctions 的表格单元格:

<ng-container matColumnDef="roleFunctions">
          <th mat-header-cell *matHeaderCellDef mat-sort-header>Functions</th>
          <td mat-cell *matCellDef="let row">
            {{ item }}
          </td>
        </ng-container>

我需要访问具有对象数组的角色函数对象。如果该数组没有任何对象,那么它也应该可以正常工作而没有任何问题。因为根据我的要求,对象可能包含也可能不包含对象数组。如果它们存在,那么我应该能够在该表 cel 中显示内部对象信息。

【问题讨论】:

    标签: angular angular-material


    【解决方案1】:

    如果我理解正确,您需要这样做才能访问表中的 roleFunctions 属性。

    <ng-container matColumnDef="roleFunctions">
      <th mat-header-cell *matHeaderCellDef mat-sort-header>Functions</th>
      <td mat-cell *matCellDef="let row">
        {{ row.roleFunctions }}
      </td>
    </ng-container>
    

    如果你想显示所有的roleFunctions,你可以这样做:

    <ng-container matColumnDef="roleFunctions">
      <th mat-header-cell *matHeaderCellDef mat-sort-header>Functions</th>
      <td mat-cell *matCellDef="let row">
        <span *ngFor="let funct of row.roleFunctions">{{func.segmentName}}</span>
      </td>
    </ng-container>
    

    【讨论】:

    • 是的,豪尔赫。你的方法对我有用。非常感谢您的回复...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    相关资源
    最近更新 更多