问题
I am using table
component in my project.In the 4th column i am displaying players names using chips
as shown in below image:
Here my issue is: if i add 2 or more names in players
string like this:
players: 'Dwayne Jhonson,Tom cruise'
both the names are displaying in the same chip
as shown in the above image,but i want each name to be displayed in separate chips
.
Something like this:
Component code: 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;
}
Here is the stackblitz link.
回答1:
If changing your model is an option try this.
Declaresymbol
property to be a string
array:
export interface PeriodicElement {
....
symbol: string[];
}
and then change your data similarly to this:
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']},
...
]
Finally on view, iterate through symbol elements to create multiple chips:
<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
If changing your model is not an option, you need to create a function that will get symbol property as argument and will return a string
array with the strings
of the original string splited by ,
. Then in the view you have to iterate on this array to generate your chips.
来源:https://stackoverflow.com/questions/52774233/to-display-mat-chips-inside-the-table-component