Пришлось сделать 2 вещи - создать новый массив, где мы сохранили массив книг в виде строки - скрыть этот столбец в 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();
}
}
завершено рабочий стекблиц здесь