简体   繁体   English

jQuery 在搜索中切换父级

[英]jQuery toggle parent in search

I have a table similar to this:我有一个类似于这样的表:

<input id="mySearch" placeholder="Search..." type="text"/>
<table>
  <th>Main header</th>
  <tbody id="searchable">
    <tr class="parent"><td>Parent</td></tr>
    <tr class="subparent"><td>Subparent one</td></tr>
    <tr class="child"><td>Child one</td></tr>
    <tr class="subparent"><td>Subparent two</td></tr>
    <tr class="child"><td>Child two</td></tr>
  </tbody>
</table>

Then what I want to do is to search through the table and show the elements where the search criteria is matched, and expand their respective subparent ("parent" is never hidden).然后我想要做的是在表格中搜索并显示搜索条件匹配的元素,并展开它们各自的子父级(“父级”永远不会隐藏)。

If I search for "child two", then both child and subparent for "child two" should be toggled, but neither child or subparent for "child one" should be toggled.如果我搜索“孩子二”,那么“孩子二”的孩子和子父母都应该被切换,但“孩子一”的孩子或子父母都不应该被切换。 A search for "child" should toggle both children and both subparents.搜索“孩子”应该同时切换孩子和两个子父母。

Here's my current script, which sort of works, but it also toggles the subparents whose children does not match the search:这是我当前的脚本,哪种有效,但它也会切换其子项与搜索不匹配的子父项:

<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
  $("#mySearch").on("keyup", function() {
    var value = $(this).val().toLowerCase();
    $("#searchable tr").filter(function() {
      $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1).prev('.subparent').toggle();
    });
  });
});
</script>

The structure of the table is as follows:表的结构如下:

Main category
`-> Main subcategory (parent)
`--> Specific subcategory (subparent)
`---> Data to search (child)

In larger scale, this translates to:在更大的范围内,这转化为:

Main category
`-> Parent 1
`--> Subparent 1
`---> Data 1
`---> Data 2
`---> Data 3
`--> Subparent 2
`---> Data 1
`---> Data 2
`-> Parent 2
`--> Subparent 1
`---> Data 1

Hence, I need to search all data, which are hidden below (sub)parent, and expand the child and subparent for all matches in search.因此,我需要搜索隐藏在(子)父项下的所有数据,并为搜索中的所有匹配项展开子项和子项。

How can I make this toggle only the subparents where the children match the search?我怎样才能使这个切换只切换到孩子匹配搜索的子父级?

You can use below script where on keyup event you need to hide all rows first and then filter child rows for which match found and if match found then show its subparent too.您可以使用下面的脚本,在 keyup 事件中,您需要先隐藏所有行,然后过滤找到匹配项的子行,如果找到匹配项,则也显示其子父行。

see below code看下面的代码

 $(document).ready(function () { $("#mySearch").on("keyup", function() { var value = $(this).val().toLowerCase(); //hide all rows $("#searchable tr.subparent, #searchable tr.child").hide(); //show rows $("#searchable tr.child").filter(function() { var found = $(this).text().toLowerCase().indexOf(value)!=-1; if(found) { // show subparent if child matches if($(this).prev().hasClass('subparent')) { $(this).prev().show(); } else { $(this).prevUntil('.subparent').last().prev().show(); } } return found; }).show(); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input id="mySearch" placeholder="Search..." type="text"/> <table> <th>Main header</th> <tbody id="searchable"> <tr class="parent"><td>Parent</td></tr> <tr class="subparent"><td>Subparent one three four</td></tr> <tr class="child"><td>Child four</td></tr> <tr class="child"><td>Child three</td></tr> <tr class="child"><td>Child one</td></tr> <tr class="subparent"><td>Subparent two five six</td></tr> <tr class="child"><td>Child two</td></tr> <tr class="child"><td>Child five</td></tr> <tr class="child"><td>Child six</td></tr> </tbody> </table>

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

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