简体   繁体   English

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

[英]Hiding Table Rows Based off selections in 2 drop down lists

I am hoping that someone here will be able to assist me, I am trying to create a list of applications and servers that can be filtered through the use of 2 drop down lists (due to there being over 100 rows worth of data in the table. 我希望这里有人能够为我提供帮助,我正在尝试创建一个应用程序和服务器列表,这些列表可以通过使用2个下拉列表进行过滤(因为该表中有100多个行的数据) 。

So far I have got the table being filtered off either or the Application Name or the Server Function, what I want to get working now is that the table filters based off BOTH selections. 到目前为止,我已经过滤掉了该表,或者是“应用程序名称”或“服务器功能”,我现在要开始工作的是,该表基于两个选择进行过滤。

So currently if I pick APP1 off the first drop down it will show all entries for APP1, however if I try to select DB Server it will show ALL applications that have DB Servers instead of just showing APP1's DB Servers. 因此,当前,如果我从第一个下拉列表中选择APP1,它将显示APP1的所有条目,但是,如果我尝试选择DB Server,它将显示具有DB Server的所有应用程序,而不仅仅是显示APP1的DB Server。

I am unable to provide the body/contents of the table due to company policy but I have provided the first row at least which would have the headers. 由于公司政策,我无法提供表格的正文/内容,但我至少提供了第一行包含标题的行。

I cannot access jfiddle or anything like that to give a working example as it is blocked by the company I work for. 我无法访问jfiddle或类似的例子作为工作示例,因为它被我工作的公司所阻止。

Hopefully the information I have provided will be enough. 希望我提供的信息就足够了。

Thanks in advance. 提前致谢。

 $(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> 

You should add some condition. 您应该添加一些条件。 The second dropdownlist match is not enough, you first check second dropdownlist match. 第二个dropdownlist匹配是不够的,您首先检查第二个dropdownlist匹配。 I have add second condition like that in if block. 我在if块中添加了第二个条件。

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

Full code: 完整代码:

$("#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();
                               }
                           });
                     });
               });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM