显示/隐藏和搜索表中的行

Show/Hide and search rows in table

本文关键字:搜索表 隐藏 显示      更新时间:2023-09-26

我有一个表,我使用quicksearchjQuery插件搜索(显示/隐藏行)。在同一个表中,若用户单击切换行按钮(按钮在表外),我必须显示/隐藏选中复选框(表中第一列)的所有行。当我在表中搜索数据时,隐藏的行(选中复选框)将变为可见。如何实现这两种功能。

当用户在表中搜索数据时,所有隐藏行(选中复选框并且用户单击了"切换行")将保持隐藏,反之亦然。

以下是我的jQuery:

$(function () {
    $('input#gridSearch').quicksearch('#<%=gvActivities.ClientID %> tbody tr');
    $('.showhiderows').click(function () {
        $('#<%=gvActivities.ClientID %>').find("input[type=checkbox]:checked").closest('tr').toggle();
    });
});

复选框是GridView中的第一列。

显示/隐藏所选行的按钮:

<input type="button" id="btnShowHide" class="showhiderows"  value="Toggle Selected Rows" />

表结构,只有标题和第一行:

<table class="gvv1" id="MainContent_gvActivities">
  <tr style="background-color: buttonface;">
      <th scope="col">
          &nbsp;
      </th>
      <th scope="col">
          Cluster
      </th>
      <th scope="col">
          Activity
      </th>
      <th scope="col">
          Data
      </th>
      <th scope="col">
          Target
      </th>
      <th scope="col">
          Achieved
      </th>
  </tr>
  <tr>
      <td>
          <input id="longIdGeneratedByCode" type="checkbox"/>
      </td>
      <td style="width: 50px;">
          ER.
      </td>
      <td style="width: 250px;">
          Establishment
      </td>
      <td style="width: 460px;">
          Number of
      </td>
      <td style="width: 70px;">
          Text
      </td>
      <td style="width: 70px;">
          Text2
      </td>
  </tr>
</table>

我想我能解决它,

$("#showhidetr").on("click", function(){
  if($(this).is(':checked')){
    $("#myTable").find("input[type='checkbox']").each(function(){
      if($(this).is(':checked')){
        $(this).parent().parent().hide();
      }
    })
  } else {
    $("#myTable").find("input[type='checkbox']").each(function(){
      if($(this).is(':checked')){
        $(this).parent().parent().show();
      }
    })
  }
})
<input type='checkbox' id="showhidetr">
<table id="myTable">
  <tr>
   <td><input type='checkbox'></td>
   <td>Mary</td>
  </tr>
  <tr>
    <td><input type='checkbox'></td>
    <td>John</td>
  </tr>
  <tr>
    <td><input type='checkbox'></td>
    <td>Michael</td>
  </tr>
</table>

单击showhidetr复选框时,脚本会在表中查找选中的复选框并隐藏其tr.