为了账号安全,请及时绑定邮箱和手机立即绑定

Jquery 多个表筛选器

Jquery 多个表筛选器

宝慕林4294392 2022-08-18 16:04:57
我正在尝试使用带有复选框的Jquery将多个过滤器应用于表格。我想对“位置和年龄”列进行筛选。位置筛选器工作正常。例如,选中“东”复选框将仅显示城市映射到“东”的行。我还需要过滤年龄列,并有一个复选框,如果选中,应该隐藏“未知”年龄。除了位置过滤器之外,还应应用此过滤器。例如,选中“隐藏未知年龄”和“东方”应仅显示东部地区有年龄的人。我将复选框的状态存储为布尔值,但我在代码中实现此内容时遇到问题。我正在考虑检查布尔值并在之前对代码进行分支(如果(cities == “”),但我认为这会导致大量重复的代码。JS小提琴:https://jsfiddle.net/qjfxgkon/$(document).ready(function () {    // Map regions to cities    const regions = {        'central': ['Chicago', 'Madison', 'Dallas'],        'east': ['New York', 'Boston'],        'west': ['Seattle', 'Los Angeles'],    }    $("input[type='checkbox']").change(function () {        var locations = [];        var hideNoAges = $('#hideAge').is(":checked")        // Get ids of each region checkbox checked        $(".location:input[type='checkbox']").each(function () {            if ($(this).is(":checked")) {                locations.push($(this).attr('id'));            }        })        // Get list of all cities to be included in filter        var cities = locations.map(function (i) { return regions[i].join(); }).join().split(",");        // Branch code here? if (!hideNoAges)..... else.......        if (cities == "") {// if no filters selected, show all items            $("#indexTable tbody tr").show();        } else { // otherwise, hide everything...            $("#indexTable tbody tr").hide();            $("#indexTable tbody tr").each(function () {                var show = false;                var row = $(this);                //show only rows that match city name                cities.forEach(function (city) {                    if (row.find('td').eq(1).text() == city) { show = true; }                })                if (show) { row.show(); }            })        }    })})
查看完整描述

1 回答

?
慕村225694

TA贡献1880条经验 获得超4个赞

你可以这样做:


$(document).ready(function() {


  // Map regions to cities

  const regions = {

    'central': ['Chicago', 'Madison', 'Dallas'],

    'east': ['New York', 'Boston'],

    'west': ['Seattle', 'Los Angeles'],

  }


  $("input[type='checkbox']").change(function() {

    var locations = [];

    var hideNoAges = $('#hideAge').is(":checked")


    // Get ids of each region checkbox checked

    $(".location:input[type='checkbox']").each(function() {

      if ($(this).is(":checked")) {

        locations.push($(this).attr('id'));

      }

    })


    // Get list of all cities to be included in filter

    var cities = locations.map(function(i) {

      return regions[i].join();

    }).join().split(",");


    if (cities == "" && !hideNoAges) { // if no filters selected, show all items

      $("#indexTable tbody tr").show();

    } else { // otherwise, hide everything...

      $("#indexTable tbody tr").hide();


      $("#indexTable tbody tr").each(function() {

        var show = false;

        var row = $(this);


        if (hideNoAges) {

          if (row.find('td').eq(2).text() == "Unknown") {

            show = false;

          } else {

            show = true;

            if (cities != "") {

              cities.forEach(function(city) {

                if (row.find('td').eq(1).text() != city) {

                  show = false;

                }

              });

            }

          }

        }


        cities.forEach(function(city) {

          if (row.find('td').eq(1).text() == city) {

            show = true;

            if (hideNoAges && row.find('td').eq(2).text() == "Unknown") {

              show = false;

            }

          }

        })

        if (show) {

          row.show();

        }

      })

    }

  })

})

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table id="indexTable">

      <thead>

        <tr>

          <th>Name</th>

          <th>Location</th>

          <th>Age</th>

        </tr>

      </thead>

      <tbody>

        <tr>

          <td>Bob</td>

          <td>Chicago</td>

          <td>24</td>

        </tr>

        <tr>

          <td>Mike</td>

          <td>New York</td>

          <td>Unknown</td>

        </tr>

        <tr>

          <td>Sarah</td>

          <td>Seattle</td>

          <td>30</td>

        </tr>

        <tr>

          <td>Bill</td>

          <td>Los Angeles</td>

          <td>51</td>

        </tr>

        <tr>

          <td>Gary</td>

          <td>Boston</td>

          <td>37</td>

        </tr>

        <tr>

          <td>Melissa</td>

          <td>Madison</td>

          <td>Unknown</td>

        </tr>

        <tr>

          <td>Greg</td>

          <td>Dallas</td>

          <td>61</td>

        </tr>

      </tbody>

    </table>

    <h5>Age Filter</h5>

    <label for="hideAge">Hide unknown ages</label>

    <input type="checkbox" id="hideAge">

    <h5>Region Filter</h5>

    <div>

      <label for="east">East</label>

      <input type="checkbox" id="east" class="location">

    </div>

    <div>

      <label for="central">Central</label>

      <input type="checkbox" id="central" class="location">

    </div>

    <div>

      <label for="west">West</label>

      <input type="checkbox" id="west" class="location">

    </div>


查看完整回答
反对 回复 2022-08-18
  • 1 回答
  • 0 关注
  • 112 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号