简体   繁体   English

如何遍历表行,更改类,并使用“n”行执行所有操作并了解每行中FIRST单元格的值

[英]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:&nbsp;</div>
                    <div style="float: left;">Alert Details:&nbsp;</div>
                    <div style="float: left;">Resource Type:&nbsp;</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: 所以这就是我需要做的事情:

  1. When the PAGE is RENDERED, I need to be able to LOOP through all the rows, no matter how many, and check the value of {{alertLevel}}... 当PAGE是RENDERED时,我需要能够循环遍历所有行,无论多少行,并检查{{alertLevel}}的值...
  2. If the ALERTLEVEL = 0 then make background-color for the FIRST TD "RED" 如果ALERTLEVEL = 0则为FIRST TD“RED”制作背景色
  3. If the ALERTLEVEL = 1 then make background-color for the FIRST TD "YELLOW" 如果ALERTLEVEL = 1,则为第一个TD“YELLOW”制作背景色
  4. If the ALERTLEVEL = 2 then make background-color for the FIRST TD "GREEN" 如果ALERTLEVEL = 2则为FIRST TD“GREEN”制作背景色

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.

相关问题 Jquery循环遍历表中没有第一行的所有行 - Jquery loop through all the rows in a table without first row DataTables:遍历所有行并从每一行的特定单元格获取值 - DataTables: loop through all rows and get value from specific cell on each row jQuery:遍历所有表行并显示具有特定类的每个单元格的弹出窗口 - JQuery: Iterate through all table rows and display popup for each cell with a certain class 想用 Puppeteer 刮桌子。 如何获取所有行,遍历行,然后为每一行获取“td”? - Want to scrape table using Puppeteer. How can I get all rows, iterate through rows, and then get "td's" for each row? 如何遍历表格行并使用 jQuery 获取单元格值 - How to iterate through a table rows and get the cell values using jQuery 循环遍历表格中所有行中的所有单元格 - Loop through all cells in all rows in a table 表中的计算字段仅针对第一行进行计算-遍历表中的行以计算列值 - Calculated Fields in table only calculating for first row - Looping through rows in table to calculate column values 如何使用querySelectorAll()查找表行的第一个单元格 - How to find first cell of table rows with querySelectorAll() 如何仅显示DataTables上的第N个第一行? 此后如何下载所有数据? - How to show just the N first number of rows on DataTables? How to download all data after this? 如何遍历 DataTables jQuery 中的所有行? - How to loop through all rows in DataTables jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM