是否可以在特定行上显示数组的第一行,然后在单独的位置(例如新表或卡)中显示数组的其余部分。
例如,我有以下数据:
public records = [{
firstName: "Joe",
lastName: "Smith",
favBooks: [ "1984", "Animal Farm", "Lord of the Rings", "Where the Sidewalk Ends"]
},
{
firstName: "Jane",
lastName: "Jameson",
favBooks: [ "Harry Potter", "Ender's Game", "Chronicles of Narnia"
},
{
firstName: "Sam",
lastName: "Baker",
favBooks: "The Pelican Brief"
}]
我希望输出显示如下:
tr.even {
background-color: #C9D4D9;
}
th#accordian-icon
td[name="accordian-icon"]
{
width: 15px;
}
th#person-name
td[name="person-name"]
{
width: 250px;
}
th#fav-book
td[name="fav-book"]
{
width: 250px;
}
<table>
<thead>
<tr>
<th id="accordion-icon"></th>
<th id="person-name">Name</th>
<th id="fav-book">Favorite Book(s)</th>
</tr>
</thead>
<tbody>
<tr>
<td name="accordion-icon">+</td>
<td name="person-name">Joe Smith</td>
<td name="fav-book">1984</td>
</tr>
<tr class="even">
<td name="accordian-icon">-</td>
<td name="person-name">Jane Jameson</td>
<td name="fav-book">Harry Potter</td>
</tr>
<tr class="even">
<td name="accordian-icon"></td>
<td name="person-name"></td>
<td name="fav-book">Ender's Game</td>
</tr>
<tr class="even">
<td name="accordian-icon"></td>
<td name="person-name"></td>
<td name="fav-book">Chronicles of Narnia</td>
</tr>
<tr class="">
<td name="accordian-icon"></td>
<td name="person-name">Sam Baker</td>
<td name="fav-book">The Pelican Brief</td>
</tr>
</tbody>
</table>
这是我到目前为止(其中accordionToggle是一个图标名称):
<table>
<thead>
<tr>
<th id="accordion-icon"></th>
<th id="person-name">Name</th>
<th id="fav-book">Favorite Book(s)</th>
</tr>
</thead>
<tbody *ngFor="let record of records; let even = even">
<tr [ngClass]="{even: even}"
(click)="onTabClick($event)">
<td name="accordion-icon">
<i class="material-icons">{{accordionToggle}}</i>
</td>
<td name="person-name">{{record.firstName}} {{record.lastName}}</td>
<td name="fav-book">{{record.favBooks[0]}}</td>
</tr>
<template [ngIf]="showBooks">
<tr *ngFor="let book of favBooks"
[ngClass]="{even:even}">
<td name="accordian-icon"></td>
<td name="person-name"></td>
<td name="fav-book">{{book}}</td>
</tr>
</template>
</tbody>
</table>
我遇到的问题是:
答案 0 :(得分:1)
<ng-container *ngIf>
而不是<template [ngIf]
record.favBooks | slice: 1
record.opened
等属性,然后点击favBooks
属性始终是一个数组(请参阅下面的ngOnInit
或使用此处所述的ArrayifyPipe
管道Does NgFor support Arrays containing one Object)。然后,您可以像record.favBooks.length > 1
这样的条件在您的视图上执行所需的逻辑以下是代码:
查看强>
<table>
<thead>
<tr>
<th id="accordion-icon"></th>
<th id="person-name">Name</th>
<th id="fav-book">Favorite Book(s)</th>
</tr>
</thead>
<tbody *ngFor="let record of records; let even = even">
<tr [ngClass]="{even: even, clickable: record.favBooks.length > 1}" (click)="record.favBooks.length > 1 ? record.opened = !record.opened : false">
<td name="accordion-icon">
<i [ngClass]="{'material-icons': record.favBooks.length > 1, opened: record.opened}"></i>
</td>
<td name="person-name">{{record.firstName}} {{record.lastName}}</td>
<td name="fav-book">{{record.favBooks[0]}}</td>
</tr>
<template [ngIf]="record.opened && record.favBooks.length > 1">
<tr *ngFor="let book of record.favBooks | slice: 1" [ngClass]="{even:even}">
<td name="accordian-icon"></td>
<td name="person-name"></td>
<td name="fav-book">{{book}}</td>
</tr>
</template>
</tbody>
</table>
<强>组件强>:
export class AppComponent {
records = [{
firstName: "Joe",
lastName: "Smith",
favBooks: ["1984", "Animal Farm", "Lord of the Rings", "Where the Sidewalk Ends"]
},
{
firstName: "Jane",
lastName: "Jameson",
favBooks: ["Harry Potter", "Ender's Game", "Chronicles of Narnia"]
},
{
firstName: "Sam",
lastName: "Baker",
favBooks: "The Pelican Brief"
}]
ngOnInit() {
this.records.forEach((x: any) => {
x.favBooks = Array.isArray(x.favBooks) ? x.favBooks : [x.favBooks];
})
}
};