Reputation: 21
I've got a Bootstrap table with dynamically loaded data that I have to filter via a dropdown and multiple inputs. I've got the inputs working, but I can't seem to figure out how to make the dropdown play nice with the inputs. When I remove the inputs, I can't seem to get the dropdown to filter. I'm honestly not quite sure what I'm doing wrong, but I've spent so many hours on this and I'm not sure what else to do.
Below is my code:
<main role="main" class="container">
<div class="container-fluid">
<div class="row">
<div class="col-md-6">
<label class="table-label mb-3 w-100">
<select id="associationSelect" class="styled-select slate float-right mb-4 table-filter search-key select-table-filter" data-table="order-table" data-column="5">
<option selected="selected" value="">Search by local board</option>
<option value="">All Boards</option>
<option>Realtor Association Name</option>
<option>Business Association</option>
<option>Company Assn</option>
<option>Company Association</option>
</select>
</label>
</div><!-- /.col-md-6 -->
<div class="col-md-6">
<input class="mb-3 w-100 search-key light-table-filter" type="search" id="firstNameInput" placeholder="First name" title="Type in a first name" data-column="0" data-table="order-table">
</div><!-- /.col-md-6 -->
</div><!-- /.col-row -->
<div class="row">
<div class="col-md-6">
<input class="mb-3 w-100 search-key light-table-filter" type="text" id="officeNameInput" placeholder="Office name" title="Type in an office name" data-column="2" data-table="order-table">
</div><!-- /.col-md-6 -->
<div class="col-md-6">
<input class="mb-3 w-100 search-key light-table-filter" type="text" id="cityTownInput" placeholder="City/Town" title="Type in a city/town name" data-column="3" data-table="order-table">
</div><!-- /.col-md-6 -->
</div><!-- /.col-row -->
<div class="row">
<div class="col-md-6">
<input class="mb-3 w-100 search-key light-table-filter" type="text" id="lastNameInput" placeholder="Last name" title="Type in a last name" data-column="1" data-table="membersTable">
</div><!-- /.col-md-6 -->
</div><!-- /.col-row -->
<div class="row">
<div class="col-12">
<table id="membersTable" class="table table-striped table-responsive-sm order-table">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Business Name</th>
<th scope="col">Address/City</th>
<th scope="col">Phone</th>
<th scope="col">Email</th>
<th class="d-none" scope="col">Association</th>
<th scope="col">Web</th>
</tr>
</thead>
<tbody>
<tr class="show-all">
<td class="pr-0 mr-0 d-inline-block" data-input="first-name">Ryan</td>
<td class="pl-0 ml-0 d-inline-block" data-input="last-name">Test</td>
<td data-input="business-name">business name</td>
<td data-input="realtor-address">1232 main, Chicago IL</td>
<td data-input="realtor-phone">555-555-5555</td>
<td data-input="realtor-email">[email protected]</td>
<td class="d-none" data-input="realtor-association">realtor association name</td>
<td data-input="web-link"><a href="http://google.com"><img src="../img/WebLink.png" alt="Web Link URL - Globe Icon" /></a></td>
</tr>
<tr class="show-all">
<td class="pr-0 mr-0 d-inline-block" data-input="first-name">test</td>
<td class="pl-0 ml-0 d-inline-block" data-input="last-name">testington</td>
<td data-input="business-name">business company</td>
<td data-input="realtor-address">1232 main, Chicago IL</td>
<td data-input="realtor-phone">555-555-5555</td>
<td data-input="realtor-email">[email protected]</td>
<td class="d-none" data-input="realtor-association">business association</td>
<td data-input="web-link"><a href="http://google.com"><img src="../img/WebLink.png" alt="Web Link URL - Globe Icon" /></a></td>
</tr>
<tr class="show-all">
<td class="pr-0 mr-0 d-inline-block" data-input="first-name">Mister</td>
<td class="pl-0 ml-0 d-inline-block" data-input="last-name">Testington</td>
<td data-input="business-name">company name</td>
<td data-input="realtor-address">1232 main, Chicago IL</td>
<td data-input="realtor-phone">555-555-5555</td>
<td data-input="realtor-email">[email protected]</td>
<td class="d-none" data-input="realtor-association">Company Assn</td>
<td data-input="web-link"><a href="http://google.com"><img src="../img/WebLink.png" alt="Web Link URL - Globe Icon" /></a></td>
</tr>
<tr class="show-all">
<td class="pr-0 mr-0 d-inline-block" data-input="first-name">Ryan</td>
<td class="pl-0 ml-0 d-inline-block" data-input="last-name">Test</td>
<td data-input="business-name">business name</td>
<td data-input="realtor-address">1232 main, Chicago IL</td>
<td data-input="realtor-phone">555-555-5555</td>
<td data-input="realtor-email">[email protected]</td>
<td class="d-none" data-input="realtor-association">realtor association name</td>
<td data-input="web-link"><a href="http://google.com"><img src="../img/WebLink.png" alt="Web Link URL - Globe Icon" /></a></td>
</tr>
<tr class="show-all">
<td class="pr-0 mr-0 d-inline-block" data-input="first-name">Ryan</td>
<td class="pl-0 ml-0 d-inline-block" data-input="last-name">Test</td>
<td data-input="business-name">business name</td>
<td data-input="realtor-address">1232 main, Chicago IL</td>
<td data-input="realtor-phone">555-555-5555</td>
<td data-input="realtor-email">[email protected]</td>
<td class="d-none" data-input="realtor-association">realtor association name</td>
<td data-input="web-link"><a href="http://google.com"><img src="../img/WebLink.png" alt="Web Link URL - Globe Icon" /></a></td>
</tr>
<tr id="noResult" class="">
<td> </td>
<td> </td>
<td colspan="" class="text-center">No Results Found</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</main><!-- /.container -->
Below is my JS:
jQuery(document).ready(function(){
$(".search-key").on('change input', function() {
var emptyFields = $('.search-key').filter(function() {
return $.trim(this.value) === "";
});
if (emptyFields.length == $(".search-key").length) {
$('table tbody tr').each(function() {
$(this).show();
});
$('#noResult').hide();
} else {
var columnNumber = $(this).data('column');
var enteredValue = $(this).val();
//var enteredValue = $(this).val();
$('table tbody tr:visible').each(function() {
var str = $(this).find('td:eq(' + parseInt(columnNumber) + ')').text();
var search = str.toLowerCase().indexOf(enteredValue.toLowerCase());
if (str.toLowerCase().indexOf(enteredValue.toLowerCase()) == -1) {
$(this).hide();
$('#noResult').show();
} else {
$(this).show();
$('#noResult').hide();
}
});
}//else
});
});
As I said, the inputs work nicely, but I can't figure out how to get the dropdown to work with them. I've tried passing the value to a string and using indexOf
to search it, but that doesn't work.
Any help is super appreciated.
Here is a fiddle I created for this: https://jsfiddle.net/VicePresidentOfAwesome/tcbLqgxp/7/
Anyone with some concept of how I can get this working?
Upvotes: 0
Views: 511
Reputation: 16440
The problem is in this line
$('table tbody tr:visible').each(function() {
// logics here is correct
});
This is how it should work:
For all changed inputs, you need to go through each table row again (no matter it's visible or hidden from the previous search)
Try the following selector
// remove `:visible` selector
$('table tbody tr').each(function() {
// logics here is correct
});
Upvotes: 1