Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
156
rated 0 times [  160] [ 4]  / answers: 1 / hits: 6150  / 9 Years ago, sun, may 17, 2015, 12:00:00

I have this table showing software logs, which gets populated by JSON data:



<div ng-controller=LogCtrl>
<table id=logtable style=width:100%>
<tr>
<th>Timestamp</th>
<th>Severity</th>
<th>Message</th>
</tr>
<tr ng-repeat=log in logs class=logentry>
<td>{{log.timestamp}}</td>
<td>{{log.severity}}</td>
<td>{{log.message}}</td>
</tr>
</table>


It works fine, but I would like to be able to change each tr element background following its severity. For example, if the severity of that log is ERROR, its entry in the table should have red background, if the severity is MESSAGE, it should be green, etc.



I already took a look at ng-style, but I didn't find out how to use it for this purpose.



Any suggestions?


More From » html

 Answers
3

You can achieve this by ng-class conditional operator



<tr ng-repeat=log in logs class=logentry 
ng-class={ 'ERROR': 'severity-error', 'MESSAGE': 'severity-message'}[log.severity]>
<td>{{log.timestamp}}</td>
<td>{{log.severity}}</td>
<td>{{log.message}}</td>
</tr>


CSS



.severity-error {
background-color: red;
}

.severity-message {
backgroud-color: green;
}

[#37078] Friday, May 15, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jazminkyrap

Total Points: 631
Total Questions: 89
Total Answers: 109

Location: Finland
Member since Fri, Oct 21, 2022
2 Years ago
;