I am implementing a filter for a table in my application.The table is filtered based on three filters region filter, role filter and active filter. The filtering works very fine based on the option selected from any one of the filters i.e. when only one dropdown has been taken into consideration.
But what I need to implement is the second dropdown should take the first dropdown into consideration and the third dropdown should take the first and second.
For example the filtering should be like below.
without any filtering applied my table looks like below
with region filter applied
while region is already selected now role has been selected
while region and role already has been selected, now active users is selected
How can I implement the the above filtering?
//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>



