我目前正在尝试实现一个搜索栏,该搜索栏将过滤掉不包含输入搜索栏中文本的行。
我当前的解决方案有效但不像我希望的那样有效,这是因为在表格的每一行中,其中一列包含另一个表格。
这是我的表格的 html:
<table class="xxxxx" id="xxxx">
<thead>
<tr class="active">
<th style="width:30%">xxx</th>
<th style="width:30%">xxx</th>
<th style="width:25%" align="right">xxx</th>
<th style="width:2%"></th>
</tr>
</thead>
<tbody id="tableBody">
<tr t-foreach="xxxxx" t-as="xxxxx">
<td>
<a "xxxxxxxxxxxxxxxxxx">
<span "xxxxxxxxx"/>
</a>
</td>
<td>
<span "xxxxxxxxxxx"/>
</td>
<td>
<span "xxxxxxxxx"/>
</td>
<td>
<table style="width:100%">
<tr>
<td>
<div>
<a>xxxxx</a>
</div>
</td>
<td>
<div>"xxxxxx"</div>
</td>
</tr>
</table>
</div>
</t>
</td>
</tr>
</tbody>
</table>
这是我当前的过滤器:
$("#search").on("keyup", function() {
var value = $(this).val().toLowerCase();
$("#tableBody tr").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
如您所见,我过滤了“tableBody”中的所有“tr”元素。这是一个问题,因为它会删除未过滤掉的行中的表,如果该行中的表又不匹配过滤器。我只想过滤第一个“tr”,而不过滤那些“tr”中的“tr”元素。
如何做到/实现?
提前致谢
最佳答案
而不是...
$("#tableBody tr")
使用...
$("#tableBody > tr")
>
意味着 tr
必须是 #tableBody
的直接子级,它将忽略所有其他 tr
树下的元素
关于javascript - 过滤包含表的表的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56576852/