I think I understand what you need to do. The idea is a column finder on the table. If you are wrong correct me.
To do this, the <input/>
fields will be in <thead>
, not <tbody>
. Another thing. There is no need to make a new request to popular the table with every keyup
of the user. Unless processing is server-side . Anyway, come on:
The working example has here
This is the Structure HTML
:
<table id="example">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
</tr>
<tr id="tr-search">
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
</tr>
</tfoot>
<tbody>
<tr class="row">
<td class="name">Tiger Nixon</td>
<td class="position">System Architect</td>
<td class="office">Edinburgh</td>
</tr>
<tr class="row">
<td class="name">Garrett Winters</td>
<td class="position">Accountant</td>
<td class="office">Tokyo</td>
</tr>
<tr class="row">
<td class="name">Ashton Cox</td>
<td class="position">Junior Technical Author</td>
<td class="office">San Francisco</td>
</tr>
<tr class="row">
<td class="name">Cedric Kelly</td>
<td class="position">Senior Javascript Developer</td>
<td class="office">Edinburgh</td>
</tr>
</tbody>
</table>
Structure CSS
.remove {
display: none;
}
Structure JavaScript
$('#example thead th').each( function () {
var title = $(this).text();
$('#tr-search').append( '<td><input type="text" class="inputs-search" name="'+title.toLowerCase()+'" placeholder="Looking for '+title.toLowerCase()+'"/></td>');
});
$('.inputs-search').on('keyup', function(){
var search = $(this).val();
var column = $(this).attr("name");
var target = '.row .' + column;
var match = new RegExp(search, "i");
$(target).each(function(){
if (!match.test($(this).text())) {
$(this).closest('tr').addClass('remove');
} else {
$(this).closest('tr').removeClass('remove');
}
});
});
To work, simply populate the bank table as you are doing it. One tip, all that was developed in the nail could have been easily developed using the plugin of JQuery datatables . That by chance, they already have this functionality ready.
I've made this example to illustrate here . Any questions, just comment.