过滤表行 - 带有全选复选框的多个过滤器

如何解决过滤表行 - 带有全选复选框的多个过滤器

首先,如果我单击表格标题中的全选复选框,整个表格行将被选中,并会显示一个复选框反消息,说明有多少个复选框我选择了。 这里,问题是如果我点击全选复选框,反消息不会显示楼上的表格,我选择了多少行。

第二,如果我从任何列中过滤任何数字,如果我选择所有复选框,则相同的数字将显示在获得过滤的行后同一列中有多少行具有相同的数字,然后计数器 -消息将显示我选中了多少行复选框。 此处,问题是显示整个表格行计数器消息,而不显示过滤的行计数器消息。

但是我正面临一个问题来解决这个问题。我该如何解决这个问题?

$( document ).ready(function() {

  $('.ckbCheckAll,.checkBoxClass').click(function () {
    if($('.ckbCheckAll:checked').length > 0 || $('.checkBoxClass:checked').length > 0) {
            $('.checkbox-count-content').show(500);
        }else{
            $('.checkbox-count-content').hide(500);
        }
    })

    const countCheckedAll = function() {
        const counter = $(".checkBoxClass:checked").length;
        $(".checkbox-count").html( counter + " variation selected!" );
        console.log(counter + ' variation selected!');
    };

    $(".checkBoxClass").on( "click",countCheckedAll );

    $('.ckbCheckAll').click(function () {
      const bulkIds = $('input[type="number').val();
      console.log(bulkIds + ' selected!');
      if(bulkIds != ''){
          bulkIds.split('/').forEach(function () {
              $('tbody').find('.checkBoxClass').prop('checked',true);
              $(this).closest('table').find('td .checkBoxClass').prop('checked',this.checked);
              countCheckedAll();
          })
      }else{
          $(".checkBoxClass").prop('checked',$(this).prop('checked'));
      }
   })

    $(".checkBoxClass").change(function(){
        if (!$(this).prop("checked")){
            $(".ckbCheckAll").prop("checked",false);
        }
    });


  const aggrFn = {
  "=": (a,b) => a == b,"<": (a,b) => a < b,">": (a,b) => a > b,"<=": (a,b) => a <= b,">=": (a,b) => a >= b,};

  function filterColumns($table) {
  const colFilters = {};
  $table.find("thead .filter").each(function() {
      colFilters[$(this).index()] = {
      agg: $(this).find("select").val(),val: $(this).find("input").val(),}
  });
  $table.find("tbody tr").each(function() {
      const $tr = $(this);
      const shouldHide = Object.entries(colFilters).some(([k,v]) => {
          return v.val === "" ? false : !aggrFn[v.agg](parseFloat($tr.find(`td:eq(${k})`).text()),parseFloat(v.val));
      });
      $tr.toggleClass("u-none",shouldHide);
  });
  }

  $(".filter").on("input",":input",function(ev) {
  filterColumns($(this).closest("table"));
  });

});
table {
    width: 100%;
    border-collapse: collapse;
    }

    td {
    text-align: center;
    padding: 10px;
    }

    table thead tr th {
      border: 1px solid #cccccc;
      padding: 10px;
    }

    table tbody tr td {
      border: 1px solid #cccccc;
    }

    .filter>div {
    display: flex;
    justify-content: center;
    }

    .filter input {
    width: 6em;
    }

    .u-none {
    display: none;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<div class="checkbox-count-content" style="display: none;">
  <div class="checkbox-count"></div>
</div>
    
<table>
  <thead>
    <th><input type="checkbox" class="ckbCheckAll" id="ckbCheckAll"></th>
    <th class="filter">
      Available Quantity
      <div>
        <select>
          <option value="=">=</option>
          <option value="<">&lt;</option>
          <option value=">">&gt;</option>
          <option value="<=">≤</option>
          <option value=">=">≥</option>
        </select>
        <input type="number">
      </div>
    </th>
    <th class="filter">
      Regular Price
      <div>
        <select>
          <option value="=">=</option>
          <option value="<">&lt;</option>
          <option value=">">&gt;</option>
          <option value="<=">≤</option>
          <option value=">=">≥</option>
        </select>
        <input type="number">
      </div>
    </th>
    <th class="filter">
      Base Price
      <div>
        <select>
          <option value="=">=</option>
          <option value="<">&lt;</option>
          <option value=">">&gt;</option>
          <option value="<=">≤</option>
          <option value=">=">≥</option>
        </select>
        <input type="number">
      </div>
    </th>
  </thead>
  <tbody>
    <tr>
      <td><input type="checkbox" class="checkBoxClass"></td> 
      <td>4</td>
      <td>10</td>
      <td>12</td>
    </tr>
    <tr>
      <td><input type="checkbox" class="checkBoxClass"></td>
      <td>6</td>
      <td>12</td>
      <td>11</td>
    </tr>
    <tr>
      <td><input type="checkbox" class="checkBoxClass"></td>
      <td>1</td>
      <td>14</td>
      <td>12</td>
    </tr>
    <tr>
      <td><input type="checkbox" class="checkBoxClass"></td>
      <td>0</td>
      <td>8</td>
      <td>10</td>
    </tr>
    <tr>
      <td><input type="checkbox" class="checkBoxClass"></td>
      <td>6</td>
      <td>14</td>
      <td>18</td>
    </tr>
    <tr>
      <td><input type="checkbox" class="checkBoxClass"></td>
      <td>1</td>
      <td>11</td>
      <td>22</td>
    </tr>
    <tr>
      <td><input type="checkbox" class="checkBoxClass"></td>
      <td>6</td>
      <td>10</td>
      <td>8</td>
    </tr>
  </tbody>
</table>

解决方法

您可以使用 :visible 检查 trs 是否可见,然后只将检查添加到 true 到仅那些 trs 复选框,然后调用您的函数以显示计数,否则只需从复选框中删除选中

演示代码

$(document).ready(function() {

  $('.ckbCheckAll,.checkBoxClass').click(function() {
    if ($('.ckbCheckAll:checked').length > 0 || $('.checkBoxClass:checked').length > 0) {
      $('.checkbox-count-content').show(500);
    } else {
      $('.checkbox-count-content').hide(500);
    }
  })

  const countCheckedAll = function() {
    const counter = $(".checkBoxClass:checked").length;
    $(".checkbox-count").html(counter + " variation selected!");
    console.log(counter + ' variation selected!');
  };

  $(".checkBoxClass").on("click",countCheckedAll);

  $('.ckbCheckAll').click(function() {
    if ($(this).is(":checked")) {
      //get tr which is visible ..add checked to that checkboxes
      $('tbody').find('tr:visible .checkBoxClass').prop('checked',true);
      countCheckedAll();
    } else {
      //remove checked
      $(".checkBoxClass").prop('checked',false);
      $('.checkbox-count-content').hide(500);
    }
  })

  $(".checkBoxClass").change(function() {
    if (!$(this).prop("checked")) {
      $(".ckbCheckAll").prop("checked",false);
    }
  });


  const aggrFn = {
    "=": (a,b) => a == b,"<": (a,b) => a < b,">": (a,b) => a > b,"<=": (a,b) => a <= b,">=": (a,b) => a >= b,};

  function filterColumns($table) {
    const colFilters = {};
    $table.find("thead .filter").each(function() {
      colFilters[$(this).index()] = {
        agg: $(this).find("select").val(),val: $(this).find("input").val(),}
    });
    $table.find("tbody tr").each(function() {
      const $tr = $(this);
      const shouldHide = Object.entries(colFilters).some(([k,v]) => {
        return v.val === "" ? false : !aggrFn[v.agg](parseFloat($tr.find(`td:eq(${k})`).text()),parseFloat(v.val));
      });
      $tr.toggleClass("u-none",shouldHide);
    });
  }

  $(".filter").on("input",":input",function(ev) {
    filterColumns($(this).closest("table"));
  });

});
table {
  width: 100%;
  border-collapse: collapse;
}

td {
  text-align: center;
  padding: 10px;
}

table thead tr th {
  border: 1px solid #cccccc;
  padding: 10px;
}

table tbody tr td {
  border: 1px solid #cccccc;
}

.filter>div {
  display: flex;
  justify-content: center;
}

.filter input {
  width: 6em;
}

.u-none {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<div class="checkbox-count-content" style="display: none;">
  <div class="checkbox-count"></div>
</div>

<table>
  <thead>
    <th><input type="checkbox" class="ckbCheckAll" id="ckbCheckAll"></th>
    <th class="filter">
      Available Quantity
      <div>
        <select>
          <option value="=">=</option>
          <option value="<">&lt;</option>
          <option value=">">&gt;</option>
          <option value="<=">≤</option>
          <option value=">=">≥</option>
        </select>
        <input type="number">
      </div>
    </th>
    <th class="filter">
      Regular Price
      <div>
        <select>
          <option value="=">=</option>
          <option value="<">&lt;</option>
          <option value=">">&gt;</option>
          <option value="<=">≤</option>
          <option value=">=">≥</option>
        </select>
        <input type="number">
      </div>
    </th>
    <th class="filter">
      Base Price
      <div>
        <select>
          <option value="=">=</option>
          <option value="<">&lt;</option>
          <option value=">">&gt;</option>
          <option value="<=">≤</option>
          <option value=">=">≥</option>
        </select>
        <input type="number">
      </div>
    </th>
  </thead>
  <tbody>
    <tr>
      <td><input type="checkbox" class="checkBoxClass"></td>
      <td>4</td>
      <td>10</td>
      <td>12</td>
    </tr>
    <tr>
      <td><input type="checkbox" class="checkBoxClass"></td>
      <td>6</td>
      <td>12</td>
      <td>11</td>
    </tr>
    <tr>
      <td><input type="checkbox" class="checkBoxClass"></td>
      <td>1</td>
      <td>14</td>
      <td>12</td>
    </tr>
    <tr>
      <td><input type="checkbox" class="checkBoxClass"></td>
      <td>0</td>
      <td>8</td>
      <td>10</td>
    </tr>
    <tr>
      <td><input type="checkbox" class="checkBoxClass"></td>
      <td>6</td>
      <td>14</td>
      <td>18</td>
    </tr>
    <tr>
      <td><input type="checkbox" class="checkBoxClass"></td>
      <td>1</td>
      <td>11</td>
      <td>22</td>
    </tr>
    <tr>
      <td><input type="checkbox" class="checkBoxClass"></td>
      <td>6</td>
      <td>10</td>
      <td>8</td>
    </tr>
  </tbody>
</table>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?