隐藏表格行基于2个下拉列表中的选择

时间:2018-10-01 13:25:27

标签: javascript jquery html html-table show-hide

我希望这里有人能够为我提供帮助,我正在尝试创建一个应用程序和服务器列表,这些列表可以通过使用2个下拉列表进行过滤(由于存在超过100行的数据)在表中。

到目前为止,我已经过滤掉了表,或者是“应用程序名称”或“服务器功能”,我现在要开始工作的是,表是基于两个选择进行过滤的。

因此,当前,如果我从第一个下拉列表中选择APP1,它将显示APP1的所有条目,但是,如果我尝试选择DB Server,它将显示所有具有DB Server的应用程序,而不仅仅是显示APP1的DB Server。

由于公司政策,我无法提供表格的正文/内容,但我至少提供了第一行带有标题的行。

我无法访问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>

1 个答案:

答案 0 :(得分:0)

您应该添加一些条件。第二个dropdownlist匹配是不够的,您首先检查第二个dropdownlist匹配。 我在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();
                               }
                           });
                     });
               });