I have an HTML table like this:
<table id="tableRegister" border="1">
<tr>
<th>Fullname</th>
<th>Age</th>
<th>Sport</th>
<th>Class </th>
<th>Term</th>
</tr>
<tr class="filter-row" data-age="11" data-class="1" data-term="Day">
<td>Thulasiram.S</td>
<td>11</td>
<td>Chess</td>
<td>1</td>
<td>Day</td>
</tr>
<tr class="filter-row" data-age="11" data-class="1" data-term="Month">
<td>ST Ram</td>
<td>11</td>
<td>Cricket</td>
<td>1</td>
<td>Month</td>
</tr>
<tr class="filter-row" data-age="21" data-class="2" data-term="Day">
<td>Ram Kumar.S</td>
<td>21</td>
<td>Chess</td>
<td>2</td>
<td>Day</td>
</tr>
<tr class="filter-row" data-age="30" data-class="3" data-term="Week">
<td>Dinesh Kumar.S</td>
<td>30</td>
<td>Chess</td>
<td>3</td>
<td>Week</td>
</tr>
</table>
I want to filter it based on three columns Age Class and Term. So the distinct values of the columns are inside three select boxes:
<select class="age" data-attribute="age" >
<option value="all">Select age</option>
<option value="11">11</option>
<option value="21">21</option>
<option value="30">30</option>
<option value="32">32</option>
</select>
<select class="class" data-attribute="class">
<option value="all">Select Class </option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
When the default option is selected in all of them I want to display all.
To make it easier during the table rendering I put three attributes in each row that contains the value of each of these three fields that are filterable for the current row: data-age="", data-class="" and data-term="".
So I want to be able to filer based on these, but I'm having a problem whether the options are reselected because it happens it hide all the rows. Can someone help me cause I'm stuck on this? Here's the [JS FIDDLE][1]
$(".age").on("change",function(){
ageVal = $(this).val();
updateTable(ageVal,'age');
});
$(".class").on("change",function(){
classVal = $(this).val();
updateTable(classVal,'class');
});
$(".term").on("change",function(){
termVal = $(this).val();
updateTable(termVal, 'term');
});
var tableRows = $('#tableRegister').find('.filter-row');
function updateTable(selectVal, attribute){
if(selectVal === 'all'){
// show all
$('.filter-row').show();
}else{
tableRows.each(function(){
var rowAttValue = $(this).attr("data-"+attribute);
if(selectVal === rowAttValue){
$(this).addClass('show');
$(this).show();
}else{
$(this).addClass('hide');
$(this).hide();
}
});
}
}