javascript - 基于对象字段值 Angular 6 的颜色 td
问题描述
我有一个表格,我想根据一个属性为这样的行着色:如果account
相同,则用灰色着色,如果不一样,用蓝色着色。这是我的代码:
func() {
for (let i = 0; i < this.List.length; i++) {
if (this.List[i].account == this.List[i + 1].account) {
this.List[i].color = "#f2f3f4"
} else {
if (this.List[i].account != this.List[i + 1].account && this.List[i].color != "#f2f3f4") {
this.List[i].color = "rgba(173, 216, 230, 0.35)"
}
}
}
}
但它不能正常工作。如何修改代码?这是一个有效的闪电战
我也收到此错误:(我猜它来自List[i + 1]
)
ERROR 错误:无法读取未定义的属性“帐户”
解决方案
基本索引错误。
您正在循环整个数组(从0
到this.List.length
),然后尝试访问this.List[i + 1]
.
循环从0
到this.List.length - 1
for (let i = 0; i < this.List.length - 1; i++) {
推荐阅读
- c++ - C/C++:提取一个枚举的子集以形成一个新的枚举
- amazon-web-services - Kubectl get svc 命令抛出拒绝访问错误
- c# - 如何创建能够序列化为动态 json 对象(如 datatables.net 配置)的 c# 类?
- docker - Docker compose bind failed: port is already allocated
- asp.net-core - Where are the ASP.NET Core/.NET Core (preview 5) sources?
- java - StatFs 方法 getTotalBytes 和 getFreeBytes 未返回正确值
- amazon-web-services - 为用户下载文件的 AWS S3 文件 URL 生成
- php - 如何使用 ajax php mysql 将 id 和表单数据发送到另一个 php 文件?
- c - 释放链表内存的问题
- c# - 一次调用在 OData 中添加多个实体关系