使用 jQuery 从多个选择元素中过滤表格

2024-04-01

我想根据用户从多个选择元素中选择的内容使用 jQuery 隐藏/显示来过滤表格。

我希望用户能够从 1、2 或 3 个选择元素中选择多个值。因此,他们可能会选择 2 名培训师、1 名新兵和 1 名状态人员,或者可能只选择 1 名培训师。计划创建一个在用户单击任何选项时运行的函数。

在我看来,每个选择元素都会有一个用户选择的值数组。因此,我需要循环遍历每个数组并将其与特定列中的文本进行比较。如果选项仅来自 1 个选择元素,那就很容易了。但由于它可能是 1、2 或全部 3,所以我很难理解它。

任何帮助将不胜感激。

Table:

<table id="reportsTable">
  <thead>
    <th>Report Number</th>
    <th>Date</th>
    <th>Name</th>
    <th>Trainer</th>
    <th>Status</th>
  </thead>
  <tbody>
    <tr>
      <td>12345-1</td>
      <td>05/01/2011</td>
      <td>First Recruit</td>
      <td>First Trainer</td>
      <td>Complete</td>
    </tr>
    <tr>
      <td>12345-2</td>
      <td>05/02/2011</td>
      <td>First Recruit</td>
      <td>Second Trainer</td>
      <td>In Progress</td>
    </tr>
    <tr>
      <td>54321-1</td>
      <td>05/03/2011</td>
      <td>Second Recruit</td>
      <td>First Trainer</td>
      <td>Created</td>
    </tr>
  </tbody>
</table>

Selects:

<select multiple="multiple" name="trainerFilter">
  <option value="firsttrainer">First Trainer</option>
  <option value="secondtrainer">Second Trainer</option>
</select>
<select multiple="multiple" name="recruitFilter">
  <option value="firstrecruit">First Recruit</option>
  <option value="secondrecruit">Second Recruit</option>
</select>
<select multiple="multiple" name="statusFilter">
  <option value="created">Created</option>
  <option value="inprogress">In Progress</option>
  <option value="complete">Complete</option>
</select>

看起来我在 8 小时内无法发布问题的答案,但这就是我想出的方法,感谢 @Spencer Ruport。由于必须考虑所有可能的条目,结果比我预期的要复杂得多。用户可以从第一个选择元素中选择某些内容,从第二个选择元素中选择任何内容,并且可能从第三个选择元素中选择 2 个。或者用户可能没有从第一个中选择任何内容,并且没有从第二个中选择任何内容。对于任何给定的输入,可能需要应用 6 个以上的过滤器。

我确信有比这更好的方法,而且看起来 @Alison 可能已经链接到一个,但它有效。

    function filterReports() {
        $('.report').hide(); //Set all rows to hidden.
        trainerVals = $('#trainerFilter').val();
        recruitVals = $('#recruitFilter').val();
        statusVals = $('#statusFilter').val();
        if (trainerVals) { //Check if any trainers are selected.
            $.each(trainerVals, function(index, trainer) {
                filtered = false; 
                classString = '';
                classString += '.' + trainer;
                if (recruitVals) { //Check if trainers and recruits are selected.
                    $.each(recruitVals, function(index, recruit) {
                        filtered = false;
                        secondString = ''; 
                        secondString = classString + '.' + recruit; //Concat to a new string so we keep the old one intact.
                        if (statusVals) { //Check if trainers, recruits and statuses are selected.
                            $.each(statusVals, function(index, status) {
                                filtered = false;
                                finalString = '';
                                finalString += secondString + '.' + status; //Again concat to a new string.
                                $(finalString).show();
                                filtered = true; //By setting filtered to true, we only run the show once.
                            });
                        }
                        if (!filtered) { //If trainers and recruits are selected, but not a status, we need to apply that filter.
                            $(secondString).show();
                            filtered = true;
                        }
                    });
                }
                if (!filtered && statusVals) { //If only trainers and statuses are selected, go through those.
                    $.each(statusVals, function(index, status) {
                        filtered = false;
                        finalString = '';
                        finalString += classString + '.' + status;
                        $(finalString).show();
                        filtered = true;
                    });
                }
                if (!filtered) { //If only trainers are selected, apply that filter.
                    $(classString).show();
                    filtered = true;
                }
            });
        }
        if (!filtered && recruitVals) { //If trainers are not selected, by recruits are, run through the recruits.
            $.each(recruitVals, function(index, recruit) {
                classString = '';
                classString += '.' + recruit;
                if (statusVals) { //Check if recruits and statuses are selected
                    $.each(statusVals, function(index, status) {
                        finalString = '';
                        finalString += classString + '.' + status;
                        $(finalString).show();
                        filtered = true;
                    });
                }
                if (!filtered) { //If only recruits are selected, apply that filter.
                    $(classString).show();
                    filtered = true;
                }
            });
        }
        if (!filtered && statusVals) { //If both trainers and recruits are not selected, but statuses are, run through those.
            $.each(statusVals, function(index, status) {
                classString = '';
                classString += '.' + status;
                $(classString).show();
                filtered = true;
            });
        }
        if (!filtered) {
            //No filters selected.
        }
        $("tr").removeClass("even"); //Remove current zebra striping.
        $("tr:visible:even").addClass("even"); //Add zebra striping only for rows that are visible.
    }

看看 jQuery数据表插件 http://www.datatables.net/。它使操作 HTML 表格变得轻而易举。通过一些简单的设置更改,您可以轻松执行您想要的操作(以及更多操作)。

请务必查看示例使用选择元素进行过滤 http://www.datatables.net/examples/api/multi_filter_select.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 jQuery 从多个选择元素中过滤表格 的相关文章

随机推荐

  • 使用 @keyup 事件输入在 Vue 中不起作用

    我试图在按 Enter 键时调用方法 但它不起作用 代码如下
  • 使用比较器对列表列表进行排序

    我正在考虑使用比较器对列表列表 在 ArrayList 上 进行排序 顺序最大的优先 所有子列表的大小始终相同 例如 一个列表 4 5 6 7 9 10 4 7 8 1 2 3 7 9 12 这应该 7 9 12 7 9 10 4 7 8
  • 调用不带括号的函数将整个函数作为字符串返回

    我创建了一个像这样的 JavaScript 对象 var obj a 10 b 20 add function return this a this b 我将函数执行为obj add它将整个函数作为字符串 a 返回 如下所示 functio
  • 我可以使用某些语法访问匿名内部类中的新方法吗?

    是否有任何 Java 语法可以从外部类访问匿名内部类中定义的新方法 我知道可以有多种解决方法 但我想知道是否存在特殊语法 例如 class Outer ActionListener listener new ActionListener O
  • XAML 文件 (WPF) 的编译

    我想了解XAML文件的编译过程 很抱歉将这个问题放在这里 但我确实没有找到任何资源深入解释这个过程 我知道 XAML 被编译成 baml 文件 但是 baml 是从生成的 g cs 文件编译而来的吗 或者 baml 是独立的 并且是从生成的
  • 是否可以使用 Sitemesh 直接在 JSP 中定义装饰器?

    我知道我应该在配置文件或我自己的子类中定义装饰器ConfigurableSiteMeshFilter 例如 public class SitemeshFilter extends ConfigurableSiteMeshFilter Ove
  • iOS/iPhone:当应用程序处于“拒绝”状态时,应用程序内购买沙箱被破坏?

    See 主要推力下面跳到我问题的主要内容 我的 iOS 应用程序在 Apple 审核过程中被拒绝 原因很简单 但很容易修复 但是 我想对新版本进行一次测试 包括重新测试我们的应用内购买 应用程序中只有一个可购买的项目 现在 应用程序在初次检
  • Python astimezone() 意外结果

    给定一个变量 其中包含巴黎时区 2000 01 01 00 01 的日期时间 据我所知 冬季为 UTC 2 datetime datetime 2000 1 1 0 1 tzinfo pytz timezone Europe Paris 我
  • jquery mobile 1.4页面多页面onpagecreate管理以避免双触发器火灾

    有人可以清除多页中事件处理程序的使用吗 文档很好 但不要警告混合使用时可能出现的冲突 例如 作为一个新手 我注意到如果我将事件处理程序放在这个 html 结构中 我会得到双重触发 该结构来自以下 或此时忽略 文档的逻辑 div div di
  • 在 PHP 中使用单引号(转义)

    我正在 PHP 标签内编写 HTML 代码 已经编写了锚标记样式 如果我更改某些部分将会影响 所以我试图在跨度 onclick 事件中编写我的代码 这是我的代码 div span array1 i name span div 如果单击该数组
  • 将几何图形转换为 BufferGeometry

    据我了解 Geometry 存储顶点和面的 javascript 对象结构 而 BufferGeometry 仅通过 Float32Arrays 等存储原始 gl 数据 有没有什么方法可以将标准 Geometry 转换为 BufferGeo
  • html5 Android 应用程序上的 Google 导航

    我只是想知道是否有人可以解释为什么这种情况发生在我身上 我正在使用phonegap 和jquerymobile 开发一个移动应用程序 该应用程序的功能之一是引导用户使用导航工具 对于 iOS 我们选择 Waze 添加它真的很容易 a hre
  • 平板电脑和手机中导航抽屉的宽度不同

    我试图为我的一个项目实施材料设计 here http www google com design spec patterns navigation drawer html 下面提到了 导航抽屉最大宽度是标准的5倍 增量 移动设备上为 56d
  • 如何将列表转换为字符串[重复]

    这个问题在这里已经有答案了 如何使用 Python 将列表转换为字符串 Use join xs 1 2 3 s join xs 如果列表包含整数 请在连接元素之前将元素转换为字符串 xs 1 2 3 s join str x for x i
  • Xcode 7,资产目录通用设备背景图片支持吗?

    我看过各种有关图像尺寸的旧帖子 但我找不到任何最新内容 甚至不知道是否可以仅使用资产目录来提供所有 iPad 和 iPhone 屏幕尺寸的图像 这是我找到的最好的帖子 但在 Xcode 7 中它没有显示 Retina 4 2x 或 iPho
  • Google 应用脚本、Gmail 中的 getBody()、正则表达式 \n

    晚上好 Google 应用程序脚本中的正则表达式换行符 n 存在此类问题 我正在使用 getbody 方法进行邮件解析并获取其中的一些 td valign middle width 43 align left img src http im
  • CoreBluetooth 应用程序在后台到底可以做什么?

    主题已经说明了一切 真的 就其存在而言 文档表明针对 iOS 设备上运行的 CoreBluetooth 框架编写的应用程序可以将 bluetooth central 添加到其后台权限列表中 从而在不活动时处理某种蓝牙事件 但exact事件是
  • OpenCL:为什么指向指针的指针不能作为参数传递给内核函数?

    你好 我只是想澄清一下为什么我们不能将 2D 数组指针作为参数传递给内核 为什么不允许 如果我使用它作为参数会发生什么 在内部 因为我知道代码会给出一些错误 请只做那些需要的 因为在 OpenCL 1 x 中设备有一个独立的地址空间 在设备
  • 包含可在源系统中定期更新的信息的事实表

    我正在构建一个维度数据仓库 并学习如何从仓库中的源系统对各种业务流程进行建模 我目前正在将数据仓库中源系统的 投标 工作投标 建模为事实表 其中包含以下信息 投标金额 预计收入 销售人员 出价状态 有效 待定 拒绝等 etc 问题在于 出价
  • 使用 jQuery 从多个选择元素中过滤表格

    我想根据用户从多个选择元素中选择的内容使用 jQuery 隐藏 显示来过滤表格 我希望用户能够从 1 2 或 3 个选择元素中选择多个值 因此 他们可能会选择 2 名培训师 1 名新兵和 1 名状态人员 或者可能只选择 1 名培训师 计划创