简体   繁体   English

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

[英]Show/Hide and search rows in table

I have a table which I search (show/hide rows) using quicksearch jQuery plugin.我有一个表格,我使用quicksearch jQuery 插件搜索(显示/隐藏行)。 In same table I have to show/hide all the rows where checkbox (first column in table) is checked, if user click toggle row button ( button is outside table).在同一个表中,如果用户单击切换行按钮(按钮在表外),我必须显示/隐藏选中复选框(表中的第一列)的所有行。 When I search data in table the hidden rows (checkbox checked) become visible.当我在表中搜索数据时,隐藏的行(选中的复选框)变得可见。 How can I achieve both functionality.我怎样才能实现这两个功能。

Where all the hidden rows (where checkbox is checked and user has clicked 'toggle rows') will remain hidden when user search data in table and vice versa.当用户在表中搜索数据时,所有隐藏行(选中复选框且用户单击“切换行”)将保持隐藏状态,反之亦然。

Following is my jQuery:以下是我的jQuery:

$(function () {
    $('input#gridSearch').quicksearch('#<%=gvActivities.ClientID %> tbody tr');

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

Checkbox is the first column in GridView.复选框是 GridView 中的第一列。

Button to show/hide selected rows:显示/隐藏选定行的按钮:

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

Table Structure, Just header and first row:表结构,只有标题和第一行:

<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>

I think I can solve it,我觉得可以解决

$("#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>

When you click showhidetr checkbox, the script looks for a checked checkbox in the table and hide its tr.当您单击 showhidetr 复选框时,脚本会在表中查找选中的复选框并隐藏其 tr。

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

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