更改特定数据的颜色

2023-12-26

我想更改 json 文件中某些数据的颜色。 如果我有一个包含日期的数据集,并且我想要 2017 年的日期为浅粉色。我怎样才能在不影响2018年、2019年等的情况下实现这一目标。日期。

json文件:

[
    {
        "id": 1,
        "month": "2017-03-01"
    }
    {
        "id": 2,
        "month": "2017-04-01"
    }
    {
        "id": 3,
        "month": "2017-05-01"
    }
    {
        "id": 4,
        "month": "2017-06-01"
    }
    {
        "id": 5,
        "month": "2017-07-01"
    }
    {
        "id": 6,
        "month": "2017-08-01"
    }
    {
        "id": 7,
        "month": "2017-09-01"
    }
    {
        "id": 8,
        "month": "2017-10-01"
    }
    {
        "id": 9,
        "month": "2017-11-01"
    }
    {
        "id": 10,
        "month": "2017-12-01"
    }
    {
        "id": 11,
        "month": "2018-01-01"
    }
    {
        "id": 12,
        "month": "2018-02-01"
    }
    {
        "id": 13,
        "month": "2018-03-01"
    }
]

这是我需要编辑的 HTML。我需要它,以便 2017 年的月份显示为浅粉色。 HTML:

<table>
  <tr>
    <th>Line 1</th>
    <th *ngFor="let volumes of volumes">{{ volumes.month | uppercase }}</th>
  </tr>
</table>

通过那个Date在每个对象到组件中,如下所示

import {
  Component
} from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  volumes: any[] = [{
      "id": 1,
      "month": "2017-03-01"
    },
    {
      "id": 2,
      "month": "2017-04-01"
    },
    {
      "id": 3,
      "month": "2017-05-01"
    },
    {
      "id": 4,
      "month": "2017-06-01"
    },
    {
      "id": 5,
      "month": "2017-07-01"
    },
    {
      "id": 6,
      "month": "2017-08-01"
    },
    {
      "id": 7,
      "month": "2017-09-01"
    },
    {
      "id": 8,
      "month": "2017-10-01"
    },
    {
      "id": 9,
      "month": "2017-11-01"
    },
    {
      "id": 10,
      "month": "2017-12-01"
    },
    {
      "id": 11,
      "month": "2018-01-01"
    },
    {
      "id": 12,
      "month": "2018-02-01"
    },
    {
      "id": 13,
      "month": "2018-03-01"
    }
  ];

  checkYear(date) {
    return new Date(date).getFullYear() == 2017 ? true : false;
  }
}
.pink-color {
  color: pink;
}
<table>
  <tr>
    <th>Line 1</th>
    <th *ngFor="let volume of volumes" [ngClass]="{ 'pink-color': checkYear(volume.month) }">
      {{ volume.id }}
    </th>
  </tr>
</table>

这里是斯塔克闪电战 https://stackblitz.com/edit/angular-ffi7jj

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

更改特定数据的颜色 的相关文章