简体   繁体   English

隐藏所有tr元素不包含带有值的td

[英]Hide all tr elements doesn't containing a td with a value

I have a table containing multiple line defined as follow: 我有一个包含多行定义如下的表:

<tr class="treegrid-1 treegrid-parent-0 trCusto" >
    <td data-column="Status" class="tdCusto cellGreen" style="text-align: center; width: 200px; " data-value="Supported">Supported</td>
    <td data-column="Reference" class="tdCusto" style="width: 100px; " data-value="123455">123455</td>
</tr>

With a single jquery line, i would like to hide the all <tr> for which we don't find a provided status value in a <td> element: 使用单个jquery行,我想隐藏所有<tr> ,我们在<td>元素中找不到提供的状态值:

$("#myTable").find("[data-value!='"+newStatusSelected+"']").parent().hide();

My code doesn't works, it hide all ceils.. 我的代码不起作用,它隐藏所有细胞..

Your code doesn't work because the rows that do have matching cells also have cells without matching values. 您的代码不起作用,因为具有匹配单元格的行具有没有匹配值的单元格。

You can do it by combining :not and :has to say "find me a tr that doesn't have a descendant with this data-value ": 你可以通过组合来实现:not:has说“找到一个没有这个data-value的后代的tr”:

$("#myTable").find("tr:not(:has([data-value='" + newStatusSelected + "']))").hide();

Example: 例:

 var newStatusSelected = "Supported"; $("#myTable").find("tr:not(:has([data-value='" + newStatusSelected + "']))").hide(); 
 <table id="myTable"> <tbody> <tr class="treegrid-1 treegrid-parent-0 trCusto"> <td data-column="Status" class="tdCusto cellGreen" style="text-align: center; width: 200px; " data-value="Supported">Supported</td> <td data-column="Reference" class="tdCusto" style="width: 100px; " data-value="123455">123455</td> </tr> <tr class="treegrid-1 treegrid-parent-0 trCusto"> <td data-column="Status" class="tdCusto cellGreen" style="text-align: center; width: 200px; " data-value="Foo">Foo</td> <td data-column="Reference" class="tdCusto" style="width: 100px; " data-value="123455">123455</td> </tr> </tbody> </table> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

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

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