angular - 在表格组件内显示 mat-chips

标签 angular angular-material

我在我的项目中使用了 table 组件。在第 4 列中,我使用 chips 显示玩家姓名,如下图所示: enter image description here

我的问题是:如果我在 players 字符串中添加 2 个或更多名称,如下所示:

  players: 'Dwayne Jhonson,Tom cruise' 

两个名字都显示在同一个 chip 中,如上图所示,但我希望每个名字都显示在 单独的 chips 中。

像这样:

enter image description here

组件代码: HTML:

<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
  <ng-container matColumnDef="position">
    <th mat-header-cell *matHeaderCellDef> No. </th>
    <td mat-cell *matCellDef="let element"> {{element.position}} </td>
  </ng-container>
  <ng-container matColumnDef="name">
    <th mat-header-cell *matHeaderCellDef> Name </th>
    <td mat-cell *matCellDef="let element"> {{element.name}} </td>
  </ng-container>
  <ng-container matColumnDef="weight">
    <th mat-header-cell *matHeaderCellDef> Weight </th>
    <td mat-cell *matCellDef="let element"> {{element.weight}} </td>
  </ng-container>
  <ng-container matColumnDef="players">
    <th mat-header-cell *matHeaderCellDef> Players </th>
    <td mat-cell *matCellDef="let element"> 
       <mat-chip-list>
              <mat-chip>{{element.players}} </mat-chip>
        </mat-chip-list>
    </td>
  </ng-container>
  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>

TS:

import {Component} from '@angular/core';

export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  players: string;
}
const ELEMENT_DATA: PeriodicElement[] = [
  {position: 1, name: 'Hydrogen', weight: 1.0079, players: 'Dwayne Jhonson,Tom cruise'},
  {position: 2, name: 'Helium', weight: 4.0026, players: 'Kevin peterson, Brett Lee'},
  {position: 3, name: 'Lithium', weight: 6.941, players: 'Sachin Tendulakar, Yuvraj Sing'},
];

@Component({
  selector: 'table-basic-example',
  styleUrls: ['table-basic-example.css'],
  templateUrl: 'table-basic-example.html',
})
export class TableBasicExample {
  displayedColumns: string[] = ['position', 'name', 'weight', 'players'];
  dataSource = ELEMENT_DATA;
}

这是 stackblitz链接。

最佳答案

如果可以选择更改模型,请试试这个。

symbol 属性声明为string 数组:

 export interface PeriodicElement {
  ....  
  symbol: string[];
}

然后像这样更改您的数据:

const ELEMENT_DATA: PeriodicElement[] = [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: ['Dwayne Jhonson','Tom cruise']},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: ['Kevin peterson', 'Brett Lee']},
...
]

最后在 View 中,迭代符号元素以创建多个筹码:

  <ng-container matColumnDef="symbol">
    <th mat-header-cell *matHeaderCellDef> Players </th>
    <td mat-cell *matCellDef="let element"> 
       <mat-chip-list>
         <span *ngFor="let symbols of element.symbol">
              <mat-chip>{{symbols}} </mat-chip>
          </span>
          </mat-chip-list>
      </td>
  </ng-container>

Demo

如果更改您的模型不是一个选项,您需要创建一个函数,它将获取符号属性作为参数并返回一个字符串数组,其中,分割的原始字符串的strings。然后在 View 中您必须迭代此数组以生成筹码。

关于angular - 在表格组件内显示 mat-chips,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52774233/

相关文章:

Angular Form 控件在组件初始化时无效,没有任何操作

css - 启动时 Angular Material Dialog 组件滚动问题

angular - 如何构造一个 Angular 库以具有多个导入路径(如 @angular/material),这样做有什么好处?

angular - 使用以下驱动的模板在 Angular 6 中创建和编辑数据的相同形式

html - 如何减小Ionic2中文本输入的大小并在右侧对齐标签

javascript - 用类声明一个变量

css - Mat-Toolbar 对齐元素(左、中、右)

angular - 如何隐藏 PrimeNG turbo 表中的列?

angularjs - 有没有办法在 Material Design Lite 中创建对话框?

css - 在 Angular Material 中生成密度是什么意思?