[英]How to loop through table rows, change class, and do it all with “n” number of rows and knowing the values of the FIRST cell in each row
OK, easy question, confusing answer... 好的,简单的问题,令人困惑的答案......
I have this code: 我有这个代码:
<tbody>
{{#each alertRow}}
<tr data-toggle="collapse" data-target="#data{{id}}" class="accordion-toggle">
<td><i id="dataCbx{{id}}" style="text-align:center;" class="icon-check center pointerCursor" title="Click here to check/uncheck the box. Click on another part of the row to expand collapse without affecting the checkbox" onclick="changeIcon(this.id);"></i></td>
<td>{{alertLevel}}</td>
<td>{{alertType}}</td>
<td>{{network}}</td>
<td>{{site}}</td>
<td>{{resource}}</td>
<td>{{component}}</td>
<td>{{details}}</td>
<td>{{alertTime}}</td>
</tr>
<tr>
<td colspan="10" class="hiddenRow">
<div id="data{{id}}" class="accordian-body collapse innerTableCollapse">
<div style="float: left;">Server Name:{{network}}</div>
<div style="float: left;">Status:{{status}}</div>
<div style="float: left;">Issue Description: </div>
<div style="float: left;">Alert Details: </div>
<div style="float: left;">Resource Type: </div>
</div>
</td>
</tr>
{{/each}}
</tbody>
Notice, I'm using HANDLEBARS.JS but that's not the problem. 请注意,我正在使用HANDLEBARS.JS,但这不是问题。
So here's what I need to do: 所以这就是我需要做的事情:
Simply enough, right? 够了,对吗?
Then, once I have that, based on the ALERT LEVEL of the FIRST COLUMN, I need to make the ENTIRE ROW (minus the first column), a LIGHT RED for ALERTLEVEL = 0, a LIGHT YELLOW for ALERTLEVEL = 1 and a LIGHT GREEN for ALERTLEVEL of 2. 然后,一旦我有了,基于第一列的ALERT LEVEL,我需要制作整个行(减去第一列),ALERTLEVEL = 0的浅红色,ALERTLEVEL = 1的浅黄色和浅绿色对于ALERTLEVEL为2。
Can you assist me?? 你能帮我吗? Please.... thanks.
拜托....谢谢。
CSS solution CSS解决方案
Define classes for your alert levels: 定义警报级别的类:
td.alertlevel0 {
background-color: #e00;
}
tr.alertlevel0 {
background-color: #f55;
}
Insert classes as you render 在渲染时插入类
<tr class="alertlevel{{alertLevel}}">
<td class="alertlevel{{alertLevel}}">{{alertLevel}}</td>
</tr>
I believe the following should work but you would need to add id="alertTable"
to your table and change: 我相信以下内容应该有效,但您需要在表中添加
id="alertTable"
并进行更改:
<td>{{alertLevel}}</td>
to: 至:
<td id="alertRow">{{alertLevel}}</td>
Additionally, I believe you would need to change your colspan 10 to colspan 9 as it seems your columns don't match up? 另外,我相信您需要将colspan 10更改为colspan 9,因为您的列似乎不匹配?
The script you would need to include would be: 您需要包含的脚本是:
<script>
$(function () {
$("#alertTable tr").each(function () {
var alertLevelElement = $("#alertLevel", this);
switch (alertLevelElement.text())
{
case "1":
alertLevelElement.css("background-color", "red");
$(this).css("background-color", "indianred");
break;
case "2":
alertLevelElement.css("background-color", "yellow");
$(this).css("background-color", "lightyellow");
break;
case "3":
alertLevelElement.css("background-color", "green");
$(this).css("background-color", "lightgreen");
break;
default:
console.error("Unrecognised alert level: " + alertLevelElement.text());
}
});
});
</script>
It would be possible to reduce this somewhat but for readability and understanding this should be sufficient. 有可能减少这一点,但为了便于阅读和理解,这应该足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.