I'm trying to make a table that can filter each column based on the values in it. I want each dropdown to be associated only with the column so like the first dropdown is only looking at the first column. I found javascript code that works perfectly but only when each value in the is unique. My table will be mostly just yes/no in every row/column and I think the reason the code I found doesn't work the way I want it is because the values aren't unique. The first column of OK/NO works but there's issues when looking at the other columns when specifying yes/no. If I select OK, no, yes from the 3 dropdowns it's supposed to only show one row which is the first but it also shows the third row. the first row is OK, no, yes and the third row is OK, no, no. So the problem is that in the third column of the third row it's no but it still appears after specifying yes from the third dropdown. Is there any way to get the dropdown to only look at a specific column? I'm still a beginner to javascript so I was just hoping to see if anyone could help while I still look into it in other sources.
here is the html
<select class="filter">
<option value="">None</option>
<option value="a">OK</option>
<option value="b">NO</option>
</select>
<select class="filter">
<option value="">None</option>
<option value="1">no</option>
<option value="2">yes</option>
</select>
<select class="filter">
<option value="">None</option>
<option value="3">no</option>
<option value="4">yes</option>
</select>
<table>
<tbody>
<tr>
<th>Status</th>
<th>Cough</th>
<th>Fever</th>
</tr>
<tr>
<td>OK</td>
<td>no</td>
<td>yes</td>
</tr>
<tr>
<td>NO</td>
<td>yes</td>
<td>yes</td>
</tr>
<tr>
<td>OK</td>
<td>no</td>
<td>no</td>
</tr>
<tr>
<td>NO</td>
<td>yes</td>
<td>no</td>
</tr>
</tbody>
</table>
and this is the javascript code I found online
$(document).ready(function () {
$('.filter').change(function () {
var values = [];
$('.filter option:selected').each(function () {
if ($(this).val() != "") values.push($(this).text());
});
filter('table > tbody > tr', values);
});
function filter(selector, values) {
$(selector).each(function () {
var sel = $(this);
var tokens = sel.text().split('\n');
var toknesObj = [], i;
for(i=0;i<tokens.length;i++){
toknesObj.push( {text:tokens[i].trim(), found:false});
}
var show = false;
console.log(values);
$.each(values, function (i, val) {
for(i=0;i<toknesObj.length;i++){
if (!toknesObj[i].found && toknesObj[i].text.search(new RegExp("\\b"+val+"\\b")) >= 0) {
toknesObj[i].found = true;
}
}
});
var count = 0;
$.each(toknesObj, function (i, val) {
if (val.found){
count+=1;
}
});
show = (count === values.length);
show ? sel.show() : sel.hide();
});
}
});
here is the link to the original javascript code http://jsfiddle.net/lukaszewczak/2dhE5/52/