根据 2 个下拉列表中的选择隐藏 Table 行

Hiding Table Rows Based off selections in 2 drop down lists

我希望这里有人能够帮助我,我正在尝试创建一个应用程序和服务器列表,可以通过使用 2 个下拉列表进行过滤(因为有超过 100 行值得table.

中的数据

到目前为止,我已经过滤掉了 table 或应用程序名称或服务器功能,我现在想要开始工作的是 table 过滤器基于两者 select离子。

所以目前,如果我从第一个下拉列表中选择 APP1,它将显示 APP1 的所有条目,但是如果我尝试 select 数据库服务器,它将显示所有具有数据库服务器的应用程序,而不仅仅是显示 APP1 的数据库服务器。

由于公司政策,我无法提供 table 的 body/contents,但我至少提供了第一行,其中包含 headers.

我无法访问 jfiddle 或类似的东西来提供一个工作示例,因为它被我工作的公司阻止了。

希望我提供的信息足够了。

提前致谢。

$(document).ready(function () {

$("#application").on("change",
               function(){
                   var appName = $(this).find("option:selected").html();

                   $("table tr td:first-child").each(
                       function(){
                           if($(this).html() != appName){
                               $(this).parent().hide();
                           }
                           else{
                               $('#titles').show();
                               $(this).parent().show();
                           }
                       });
               });

$("#function").on("change",
               function(){
                   var functionName = $(this).find("option:selected").html();

                   $("table tr td:first-child").next().each(
                       function(){
                           if($(this).html() != functionName){
                               $(this).parent().hide();
                           }
                           else{
                               $('#titles').show();
                               $(this).parent().show();
                           }
                       });
               });
               });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div title="Select Application"">
<strong>Select Application</strong><br>
<select id="application" class="form-control selectpicker">
    <option value="">--Select--</option>
    <option value="">APP1</option>
    <option value="">APP2</option>
    <option value="">APP3</option>
    <option value="">APP4</option>
    <option value="">APP5</option>
    <option value="">APP6</option>
    <option value="">APP7</option>
    <option value="">APP8</option>
    <option value="">APP9</option>
    <option value="">APP10</option>
    <option value="">APP11</option>
    <option value="">APP12</option>
</select>
</div>
<br>
<div title="Select Server Function"">
<strong>Select Application</strong><br>
<select id="function" class="form-control selectpicker">
    <option value="">--Select--</option>
    <option value="">DB Server</option>
    <option value="">Automate Job Server</option>
    <option value="">Web Server</option>
    <option value="">Application Server</option>
    <option value="">Database & Application Server</option>
    <option value="">Citrix Server</option>
    <option value="">Inbound/Outbound Server</option>
    <option value="">COMMS Server</option>
    <option value="">WEBFARM Servers</option>
    <option value="">WAS Instances</option>
    <option value="">APP/Web Server</option>
    <option value="">Autosys</option>
    <option value="">Qlik Sense Server</option>
</select>
</div>
<br>
<table style="width: 733.38px;" class=">
<tbody>
<tr style="height: 20px;" id="titles";>
<td style="width: 147px; height: 20px;"><strong>Application Name</strong></td>
<td style="width: 162px; height: 20px;"><strong>Function</strong></td>
<td style="width: 256px; height: 20px;"><strong>Server Name</strong></td>
<td style="width: 138.38px; height: 20px;"><strong>ENVIRONMENT</strong></td>
</tr>
<tr style="height: 20px;">
<td style="width: 147px; height: 20px;">APP1</td>
<td style="width: 162px; height: 20px;">DB Server</td>
<td style="width: 256px; height: 20px;">ServerName</td>
<td style="width: 138.38px; height: 20px;">PROD</td>
</tr>
<tr style="height: 20.5px;">
<td style="width: 147px; height: 20.5px;">APP1</td>
<td style="width: 162px; height: 20.5px;">App Server</td>
<td style="width: 256px; height: 20.5px;">ServerName</td>
<td style="width: 138.38px; height: 20.5px;">COB</td>
</tr>
<tr style="height: 20px;">
<td style="width: 147px; height: 20px;">APP2</td>
<td style="width: 162px; height: 20px;">DB Server</td>
<td style="width: 256px; height: 20px;">ServerName</td>
<td style="width: 138.38px; height: 20px;">UAT</td>
</tr>
<tr style="height: 20px;">
<td style="width: 147px; height: 20px;">APP3</td>
<td style="width: 162px; height: 20px;">DB Server</td>
<td style="width: 256px; height: 20px;">ServerName</td>
<td style="width: 138.38px; height: 20px;">PROD</td>
</tr>

你应该添加一些条件。第二个下拉列表匹配项不够,您首先检查第二个下拉列表匹配项。 我在 if 块中添加了第二个条件。

$("#application").find("option:selected").html() != $(this).prev().html();

完整代码:

$("#function").on("change",
               function(){
                   var functionName = $(this).find("option:selected").html();
                   $("table tr td:first-child").next().each(
                           function(){
                               if($(this).html() != functionName || $("#application").find("option:selected").html() != $(this).prev().html()){
                                   $(this).parent().hide();
                               }
                               else{
                                   $('#titles').show();
                                   $(this).parent().show();
                               }
                           });
                     });
               });