[英]Compare two cells in a html table and highlight row if different
I have an HTML table (tree table precisely but it doesn't matter) and it has several columns.我有一个 HTML 表(准确地说是树表,但没关系),它有几列。 For each row, it is very important that the values in one of the columns should be higher than the other column.
对于每一行,其中一列中的值应高于另一列中的值非常重要。 If that is not the case then I'd like to highlight that entire row.
如果不是这种情况,那么我想突出显示整行。 How do I do that?
我怎么做?
My HTML code looks like this:我的 HTML 代码如下所示:
<table id="stepstats-list-exp">
<thead>
<tr>
<th> name </th>
<th> elp_01 </th>
<th> elp_20 </th>
<th> scal </th>
</tr>
</thead>
<tbody>
<tr data-tt-id=864845 data-tt-parent-id=>
<td> 'Init' </td>
<td class="elp_01"> 0 </td>
<td class="elp_20"> 0 </td>
<td class="scal"> 0.00 </td>
</tr>
<tr data-tt-id=864846 data-tt-parent-id=864845>
<td> 'Update' </td>
<td class="elp_01"> 0 </td>
<td class="elp_20"> 0 </td>
<td class="scal"> 0.00 </td>
</tr>
<tr data-tt-id=864847 data-tt-parent-id=>
<td> 'Load' </td>
<td class="elp_01"> 32 </td>
<td class="elp_20"> 31 </td>
<td class="scal"> 1.03 </td>
</tr>
</tbody>
</table>
In all my test cases, elp_20
should always be smaller than elp_01
.在我所有的测试用例中,
elp_20
应该总是小于elp_01
。 If not, the entire row needs to be highlighted.如果不是,则需要突出显示整行。 For that purpose, I have this jQuery code that doesn't seem to be working.
为此,我有这个 jQuery 代码似乎不起作用。 For each
tr
row, I'm checking each td
column and comparing values.对于每个
tr
行,我正在检查每个td
列并比较值。
<script type="text/javascript">
/* Highlight row if elp_20 > elp_01 */
$(document).ready(function () {
$("#stepstats-list-exp tr").each(function () {
$(this).find('td').each(function(){
if (parseInt($(".elp_20").text(), 10) < parseInt($(".elp_01").text(), 10)) {
$(this).parent("tr").css('background-color', 'crimson');
$(this).parent("tr").css('font-weight','bold');
$(this).parent("tr").css('color','white');
}
});
});
});
</script>
This working snippet accesses the table rows as an html collection
that can be looped through to compare the values represented by the contents of the second and third cell of each row.此工作代码段以
html collection
的形式访问表行,可以循环访问该集合以比较每行的第二个和第三个单元格的内容表示的值。 An inline style attribute it used to highlight the row (alternative styling could be made by adding or toggling class names if something more complex is needed)它用于突出显示行的内联样式属性(如果需要更复杂的东西,可以通过添加或切换 class 名称来制作替代样式)
let tableRows = document.getElementById("stepstats-list-exp").getElementsByTagName('tr'); for (let i=0; i<tableRows.length; i++) if (Number(tableRows[i].children[2].innerText) >= Number(tableRows[i].children[1].innerText)) { tableRows[i].setAttribute("style", "background: yellow"); }
<table id="stepstats-list-exp"> <thead> <tr> <th> name </th> <th> elp_01 </th> <th> elp_20 </th> <th> scal </th> </tr> </thead> <tbody> <tr data-tt-id=864845 data-tt-parent-id=> <td> 'Init' </td> <td class="elp_01"> 0 </td> <td class="elp_20"> 0 </td> <td class="scal"> 0.00 </td> </tr> <tr data-tt-id=864846 data-tt-parent-id=864845> <td> 'Update' </td> <td class="elp_01"> 0 </td> <td class="elp_20"> 0 </td> <td class="scal"> 0.00 </td> </tr> <tr data-tt-id=864847 data-tt-parent-id=> <td> 'Load' </td> <td class="elp_01"> 32 </td> <td class="elp_20"> 31 </td> <td class="scal"> 1.03 </td> </tr> </tbody> </table>
You can just iterate through the classes, add them to a variable and iterate with a for loop, if you need to iterate one elp_01
to all elp_20
just map it.您可以只遍历类,将它们添加到变量并使用 for 循环进行迭代,如果您需要将一个
elp_01
迭代到所有elp_20
只需 map 即可。
Here's an example:这是一个例子:
let firstColumn = document.getElementsByClassName('elp_01').innerText
let secondColumn = document.getElementsByClassName('elp_20').innerText
for (let i = 0; i < firstColumn.length; i ++) {
if (firstColumn[i] > secondColumn[i]) {
// Something - maybe add a class in css to color the row and add it to the element
}
else {
// Something else
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.