有什么办法可以在*ngFor
中制作<mat-cell>
?
我需要在材质中添加新列并将其隐藏,我仅将其用于过滤目的...
我有这样的桌子
<ng-container matColumnDef="email">
<mat-header-cell *matHeaderCellDef mat-sort-header> Email </mat-header-cell>
<mat-cell *matCellDef="let user"> {{ user.email }} </mat-cell>
</ng-container>
<ng-container matColumnDef="phone">
<mat-header-cell *matHeaderCellDef mat-sort-header> Phone </mat-header-cell>
<mat-cell *matCellDef="let user"> {{ user.phone }} </mat-cell>
</ng-container>
但是现在我需要用
{{ user.book.name}}
换一行,这种方式当然行不通。我需要这样的东西
<ng-container *ngFor="let book of user.book" [matColumnDef]="book">
<th mat-header-cell *matHeaderCellDef mat-sort-header> {{ book}} </th>
<td mat-cell *matCellDef="let book"> {{ book.name }} </td>
</ng-container>
但这无法正常工作,我为此破坏了所有应用程序...
这是stackblitz上的示例,这是我需要获取图书名称的对象在哪里
最佳答案
不得不做两件事
-创建一个新数组,我们将书本数组存储为字符串
-在HTML中隐藏此列(您可以删除display:none
进行测试
相关的HTML:
<!-- Books Column -->
<ng-container matColumnDef="books" >
<th mat-header-cell *matHeaderCellDef style='display:none'> Books </th>
<td mat-cell *matCellDef="let element" style='display:none'> {{ element.books }} </td>
</ng-container>
相关TS:
import {Component} from '@angular/core';
import {MatTableDataSource} from '@angular/material/table';
export interface PeriodicElement {
name: string;
position: number;
weight: number;
symbol: string;
books: any[];
}
const ELEMENT_DATA: PeriodicElement[] = [
{position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H', books: [{name: "book4"},{name: "book2"}, {name: "book3"}]},
{position: 2, name: 'Helium', weight: 4.0026, symbol: 'He', books: [{name: "book6"},{name: "book2"}, {name: "book3"}]},
{position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li', books: [{name: "book5"},{name: "book2"}, {name: "book3"}]},
{position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be', books: [{name: "book7"},{name: "book2"}, {name: "book3"}]},
{position: 5, name: 'Boron', weight: 10.811, symbol: 'B', books: [{name: "book1"},{name: "book2"}, {name: "book3"}]},
{position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C', books: [{name: "book1"},{name: "book2"}, {name: "book3"}]},
{position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N', books: [{name: "book1"},{name: "book2"}, {name: "book3"}]},
{position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O', books: [{name: "book1"},{name: "book2"}, {name: "book3"}]},
{position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F', books: [{name: "book1"},{name: "book2"}, {name: "book3"}]},
{position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne', books: [{name: "book1"},{name: "book2"}, {name: "book3"}]},
];
/**
* @title Table with filtering
*/
@Component({
selector: 'table-filtering-example',
styleUrls: ['table-filtering-example.css'],
templateUrl: 'table-filtering-example.html',
})
export class TableFilteringExample {
newVar: PeriodicElement[] =[];
constructor(){
for(var i=0;i<ELEMENT_DATA.length; i++){
this.newVar.push(
{position: ELEMENT_DATA[i].position, name: ELEMENT_DATA[i].name, weight: ELEMENT_DATA[i].weight, symbol: ELEMENT_DATA[i].symbol, books: [JSON.stringify(ELEMENT_DATA[i].books)] }
);
}
console.log(this.newVar);
}
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol', 'books'];
dataSource = new MatTableDataSource(this.newVar);
applyFilter(filterValue: string) {
this.dataSource.filter = filterValue.trim().toLowerCase();
}
}
完成working stackblitz here
关于css - Angular Material * ng用于<mat-cell>内部进行搜索,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58446025/