我试图按如下所示设置动态值的字体颜色:如果为incident.state_id = 1
,则为红色字体,如果为incident.state_id = 2
,则为黄色字体,如果为incident.state_id = 3
,则为红色字体。
incident.state_id = 1 : state.name = "Pending Investigation"
incident.state_id = 2 : state.name = "Investigation in Progress"
incident.state_id = 3 : state.name = "Investigation Closed"
我采用了下面的coffeescript片段,但我对js / coffee知之甚少,所以我不知道要进行哪些更改才能使此工作有效。
当前,所有状态字段均显示为红色。
events.js.coffee:
$(".state").each ->
$this = $(this)
value = $this.text()
if value = 1
$this.addClass "red"
else if value = 2
$this.addClass "yellow"
else
$this.addClass "green"
application.html.erb
td.state {
}
.red {
color: #f99;
}
.yellow {
color: #ff9;
}
.green {
color: #9f9;
}
events.html.erb:
<td class="state"><%= incident.state.name %></td>
最佳答案
为什么要烦恼咖啡脚本(为此)?
相反,我建议在erb文件中设置正确的类,如下所示:
<% if incident.state_id == 1 %>
<td class="red"><%= incident.state.name %></td>
<% elsif incident.state_id == 2 %>
<td class="yellow"><%= incident.state.name %></td>
<% else %>
<td class="green"><%= incident.state.name %></td>
<% end %>
这可能不是最干净的方法,但是应该解决当前的问题。