gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 01:51:35 27 4
gpt4 key购买 nike

我在我的项目中使用了 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com