简体   繁体   English

比较 html 表中的两个单元格,如果不同则突出显示行

[英]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> &#39;Init&#39; </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> &#39;Update&#39; </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> &#39;Load&#39; </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> &#39;Init&#39; </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> &#39;Update&#39; </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> &#39;Load&#39; </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.

相关问题 比较具有相同字段的两个数组 => 如果值不同,则在 html 表中突出显示 - Compare two array with same fields => if value is different highlight in html table 比较两行(HTML表)数据(即行[i]与行[i + 1]),并使用Jquery / Javascript突出显示更改 - Compare two rows (HTML Table) data i.e..row[i] with row[i+1] and highlight changes using Jquery/Javascript 如何比较html表的两列值并用颜色突出显示不匹配的数据? - how to compare html table two columns values and highlight the unmatched data with color? 根据列的不同,用不同的颜色突出显示表格行 - Highlight table row with different colors depending on their column 用php突出显示html表中的选定行 - Highlight the selected row in html table with php 如何在 html 中突出显示单击表格的行 - How to highlight row on click on table in html 在html表格中突出显示鼠标悬停时选定的行和列 - highlight selected row and column on mouseover in html table 突出显示 HTML 表格行数据的差异 - Highlight differences in row data of HTML table 将鼠标悬停在一个表格单元格中将突出显示一行中的前一个单元格 - Hovering on one table cell will highlight previous cells in a row 扩展 html 表格突出显示行和列并使当前单元格具有不同的颜色 - Extend html table highlight row and column and make current cell a different color
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM