简体   繁体   English

如果另一个单元格包含值,则向表格单元格添加类

[英]Add class to a table cell if another cell contains a value

I would like to add via javascript a class to a table's cell if another cell into another column contains a certain value.如果另一列中的另一个单元格包含某个值,我想通过 javascript 将一个类添加到表格的单元格中。 I am using a plugin to generate the table and datas comes from a JSON file, so I cannot edit the HTML code of the table.我正在使用插件生成表格,数据来自 JSON 文件,因此我无法编辑表格的 HTML 代码。

For example if a cell in the "Company" column contains "Ernst Handel" I would like to add a class .custom-class to another cell in the same tr , in my case to the cell "Austria" in the column Country.例如,如果“公司”列中的单元格包含“Ernst Handel”,我想将类.custom-class添加到同一tr中的另一个单元格,在我的情况下添加到列国家/地区中的单元格“奥地利”。

What I tried is:我试过的是:

$('tr').each(function(){
   if($('td:contains("Ernst Handel")', this).length && $('td:contains("Austria")', this).length){       
        alert('here');
        $(this).addClass('custom-class');
    }
});

Here a sample:这里有一个示例:

<table>
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
  <tr>
    <td>Ernst Handel</td>
    <td>Roland Mendel</td>
    <td>Austria</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>Helen Bennett</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Yoshi Tannamuri</td>
    <td>Canada</td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Giovanni Rovelli</td>
    <td>Italy</td>
  </tr>
</table>

CSS CSS

table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}

.custom-class {
  border: 1px solid red;
}

here a jsFiddle这里有一个jsFiddle

Many thanks in advance提前谢谢了

In this line:在这一行:

$(this).addClass('custom-class');

this points to the whole row, not the td , so basically you are adding the class name to the tr element. this指向整行,而不是td ,因此基本上您将类名添加到tr元素。 If the cell you want to select is always in the third column like your example, then one way to target it is by using nth-child :如果您要选择的单元格始终像您的示例一样位于第三列中,那么定位它的一种方法是使用nth-child

$(this).find("td:nth-child(3)").addClass('custom-class');

 $('tr').each(function(){ if($('td:contains("Ernst Handel")', this).length && $('td:contains("Austria")', this).length){ alert('here'); $(this).find("td:nth-child(3)").addClass('custom-class'); } });
 table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; } td, th { border: 1px solid #dddddd; text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #dddddd; } .custom-class { border: 1px solid red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr> <th>Company</th> <th>Contact</th> <th>Country</th> </tr> <tr> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Germany</td> </tr> <tr> <td>Centro comercial Moctezuma</td> <td>Francisco Chang</td> <td>Mexico</td> </tr> <tr> <td>Ernst Handel</td> <td>Roland Mendel</td> <td>Austria</td> </tr> <tr> <td>Island Trading</td> <td>Helen Bennett</td> <td>UK</td> </tr> <tr> <td>Laughing Bacchus Winecellars</td> <td>Yoshi Tannamuri</td> <td>Canada</td> </tr> <tr> <td>Magazzini Alimentari Riuniti</td> <td>Giovanni Rovelli</td> <td>Italy</td> </tr> </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM