我有如下的JSON,但我有很多数据:
物品:{ itemId:"22222",类别:'A',总数: 100 },{ itemId:"666666",类别:'A',总数: 80 },{ itemId:"555",类别:'B',总数: 50 } ....... { itemId:"555",分类:'B',总计:2}
我在.scss上创建
&.is-green {
color: green;
}
&.is-red {
color: red;
}我想像这样使用它:
<div *ngFor="let item of items;>
<div>{{item.category}}</div>
<div
[ngClass]="{
'is-green': item.total ,
'is-red':item.total
}"
>
{{item.total}}</div>
</div>从这个数据中,我想找到最小的total,并将颜色改为绿色。我也想找到最大的total和改变颜色红色。
你知道怎么做吗?
发布于 2021-10-13 16:25:11
所以,这个问题有两个部分,对吗?第一个是sorting,第二个是changing the first and last items green and red。
对于排序,您可以使用以下命令
items = items.sort((fstItem, secItem)=> fstItem.total > secItem.total ? 1 :
fstItem.total < secItem.total -1 : 0 );第二件事是,您可以在html中使用来自*ngFor的第一个和最后一个索引
<div *ngFor="let item of items; first as isFirstItem; last as isLastItem;">
<div>{{item.category}}</div>
<div [ngClass]="{'is-green': isFirstItem,
'is-red': isLastItem
}">
{{item.total}}
</div>
</div>https://stackoverflow.com/questions/69558250
复制相似问题