【发布时间】:2019-09-20 10:56:07
【问题描述】:
我有一个如下所示的对象数组:
newItems = [
{ id: 8997, yellow: 'Y', red: 'N', reportId: 1 }, //HewlettPackard
{ id: 8997, yellow: 'N', red: 'N', reportId: 2 }, //HewlettPackard
{ id: 6300, yellow: 'N', red: 'Y', reportId: 2 }, //EpsonCenter5000
{ id: 0019, yellow: 'Y', red: 'N', reportId: 1 } //another report
]
我正在使用 ngFor 遍历对象,它工作正常。基本上,如果在一个对象中,黄色或红色设置为“Y”,则显示相应的色环。
一个类别 ID (id) 可以显示大约 4 个报告 (reportID)。
<div *ngFor="let loop of newItems">
<table>
<ng-template *ngIf="loop.yellow = 'Y' && loop.red == 'N' && loop.reportId == 1">
<tr>
<td class="yellow">Polaris South</td>
</tr>
</ng-template>
<ng-template *ngIf="loop.red = 'Y' && loop.yellow == 'N' && loop.reportId == 1">
<tr>
<td class="red">Polaris South</td>
</tr>
</ng-template>
<ng-template *ngIf="loop.reportId !== 1">
<tr>
<td class="green">Polaris South</td>
</tr>
</ng-template>
***********************
<ng-template *ngIf="loop.yellow = 'Y' && loop.red == 'N' && loop.reportId == 2">
<tr>
<td class="yellow">Dandride</td>
</tr>
</ng-template>
<ng-template *ngIf="loop.red = 'Y' && loop.yellow == 'N' && loop.reportId == 2">
<tr>
<td class="red">Dandride</td>
</tr>
</ng-template>
<ng-template *ngIf="loop.reportId !== 2">
<tr>
<td class="green">Dandride</td>
</tr>
</ng-template>
********************
<ng-template *ngIf="loop.yellow = 'Y' && loop.red == 'N' && loop.reportId == 3.1">
<tr>
<td class="yellow">Opmanual Internal</td>
</tr>
</ng-template>
<ng-template *ngIf="loop.red = 'Y' && loop.yellow == 'N' && loop.reportId == 3.1">
<tr>
<td class="red">Opmanual Internal</td>
</tr>
</ng-template>
<ng-template *ngIf="loop.reportId !== 3.1">
<tr>
<td class="green">Opmanual Internal</td>
</tr>
</ng-template>
**************************
<ng-template *ngIf="loop.yellow = 'Y' && loop.red == 'N' && loop.reportId == 3.2">
<tr>
<td class="yellow">Zetaphi Remarks</td>
</tr>
</ng-template>
<ng-template *ngIf="loop.red = 'Y' && loop.yellow == 'N' && loop.reportId == 3.2">
<tr>
<td class="red">Zetaphi Remarks</td>
</tr>
</ng-template>
<ng-template *ngIf="loop.reportId !== 3.2">
<tr>
<td class="green">Zetaphi Remarks</td>
</tr>
</ng-template>
</table>
</div>
问题是,对于每个类别(即 Hewlett Packard、Epson Center 5000)所在的框,如果为不同的 ReportId 找到黄色和红色值,因为 for 循环循环遍历对象的迭代,您会得到重复的结果如果有多个报告(单个 id 的报告 ID)。见截图。
我的目标是,如果黄色设置为“Y”,则在 id 匹配的任何迭代中显示它,红色相同但不重复,相反,如果找到“Y”,则覆盖绿色默认值。
【问题讨论】:
-
嗨,下面的解决方案对你有用吗?
标签: css angular typescript angular-directive ng-template