I needed a way to quickly distinguish between “good” and “bad” years, that led me to NgClass. So let’s continue on with Angular basics - NgClass.
I’d like the cell to be shaded green when I’m making money on the solar panels (any positive number) and red when the panels are costing me more than just what the base utility cost would be (any negative number). With that, it was just a few steps:
Write CSS to represent the color change
Write a Typescript function to determine which class should be applied to a table cell
Write the NgClass logic within the view
CSS
1 2 3 4 5 6 7
.positive { background-color: lightgreen; }
.negative { background-color: lightcoral; }
Typescript
1 2 3 4 5 6 7 8
public inTheGreen(value: number): boolean{ if (value >= 0) { returntrue; }
I’m not sure if there’s a way to combine the inTheGreen and !inTheGreen checks into a single call, or it if much matters, but it’s a little lengthy right now.
Next time, perhaps I’ll start digging into implementing some charting and/or utilizing a datagrid so it’s a little “prettier” and more interactive.