【发布时间】:2018-06-12 17:38:32
【问题描述】:
我正在为我的应用程序中的表实现过滤器。该表基于三个过滤器进行过滤:region filter、role filter 和 active 过滤。 根据从任一过滤器中选择的选项,即仅考虑一个下拉菜单时,过滤效果非常好。
但我需要实现的是第二个下拉菜单应该考虑第一个下拉菜单,第三个下拉菜单应该考虑第一个和第二个。
例如过滤应该如下所示。
没有应用任何过滤,我的表格如下所示
应用了区域过滤器
虽然已经选择了地区,但现在已经选择了角色
虽然已经选择了地区和角色,但现在选择了活跃用户
如何实现上述过滤?
//Filtering region dropdown
$('body').on("change", '#regionDropdown', function() {
var filter, table, tr, td, i;
filter = $(this).val();
table = document.getElementById("download-forms-table-tbody");
tr = table.getElementsByTagName("tr");
if (filter == "All") {
// Loop through all table rows, and show anyrows that is hidden
for (i = 0; i < tr.length; i++) {
tr[i].style.display = "";
}
} else {
// Loop through all table rows, and hide those who don't match the search query
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[1];
if (td) {
if (td.innerHTML.indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
} else {
var a = "No Records Found!!!";
}
}
}
});
//Filtering role dropdown
$('body').on("change", '#roleDropdown', function() {
var filter, table, tr, td, i;
filter = $(this).val();
table = document.getElementById("download-forms-table-tbody");
tr = table.getElementsByTagName("tr");
if (filter == "All") {
// Loop through all table rows, and show anyrows that is hidden
for (i = 0; i < tr.length; i++) {
tr[i].style.display = "";
}
} else {
// Loop through all table rows, and hide those who don't match the search query
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[2];
if (td) {
if (td.innerHTML.indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
});
//Show active inactive users
$('body').on("change", '#associateStatusDropdown', function() {
var filter, table, tr, td, i;
filter = $(this).val();
table = document.getElementById("download-forms-table-tbody");
tr = table.getElementsByTagName("tr");
if (filter == "All") {
// Loop through all table rows, and show anyrows that is hidden
for (i = 0; i < tr.length; i++) {
tr[i].style.display = "";
}
} else {
// Loop through all table rows, and hide those who don't match the search query
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[3];
if (td) {
if (td.innerHTML.indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
} else {
var a = "No Records Found!!!";
}
}
}
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="row">
<div class="col-4">
<select id="regionDropdown">
<option value="All">All Region</option>
<option value="Asia Pacific">Asia Pacific</option>
<option value="Continental Europe">Continental Europe</option>
<option value="North America">North America</option>
<option value="United Kingdom">United Kingdom</option>
</select>
</div>
<div class="col-4">
<select id="roleDropdown">
<option value="All">All Roles</option>
<option value="Account Executive">Account Executive</option>
<option value="Archer">Archer</option>
<option value="Sales Manager">Sales Manager</option>
<option value="SET Executive">SET Executive</option>
</select>
</div>
<div class="col-4">
<select id="associateStatusDropdown">
<option value="All"> All Users </option>
<option value="Yes">Active Users</option>
<option value="No">Inactive Users</option>
</select>
</div>
</div>
<table class="table">
<thead>
<tr>
<th> SL.NO </th>
<th> Region </th>
<th> Role </th>
<th> Active </th>
</tr>
</thead>
<tbody id="download-forms-table-tbody">
<tr>
<td> 1 </td>
<td> North America </td>
<td> Account Executive </td>
<td> No </td>
</tr>
<tr>
<td> 2 </td>
<td> Continental Europe </td>
<td> Archer </td>
<td> Yes </td>
</tr>
<tr>
<td> 3 </td>
<td> Continental Europe </td>
<td> Account Executive </td>
<td> No </td>
</tr>
<tr>
<td> 4 </td>
<td> North America </td>
<td> Account Executive </td>
<td> Yes </td>
</tr>
<tr>
<td> 5 </td>
<td> Continental Europe </td>
<td> Sales Manager </td>
<td> No </td>
</tr>
<tr>
<td> 6 </td>
<td> Asia Pacific </td>
<td> Account Executive </td>
<td> yes </td>
</tr>
<tr>
<td> 7 </td>
<td> North America </td>
<td> SET Executive </td>
<td> No </td>
</tr>
<tr>
<td> 8 </td>
<td> United Kingdom </td>
<td> Archer </td>
<td> Yes </td>
</tr>
<tr>
<td> 9 </td>
<td> Continental Europe </td>
<td> Archer </td>
<td> No </td>
</tr>
<tr>
<td> 10 </td>
<td> Asia Pacific </td>
<td> SET Executive </td>
<td> Yes </td>
</tr>
</tbody>
</table>
</div>
【问题讨论】:
-
使用单个事件处理程序根据所选值过滤数据
-
@Satpal,确实如此,但它会重置旧的选择。
标签: javascript jquery html