Lets say we have the following html table (with columns Product, category and Customer)
<input type="text" id="order_search" value="" />
<table border="1" padding="5" width="100%">
<thead>
<tr>
<th>Product</th>
<th>Category</th>
<th>Customer</th>
</tr>
</thead>
<tbody>
<tr>
<td>Bananas</td>
<td>fruits</td>
<td>Al pacino</td>
</tr>
<tr>
<td>Pork</td>
<td>Meat</td>
<td>Nick the americans</td>
</tr>
<tr>
<td>Juice</td>
<td>Beverages</td>
<td>The kingo</td>
</tr>
<tr>
<td>Nike SB</td>
<td>Shoes</td>
<td>Nike</td>
</tr>
</tbody>
We will create function to create and assign a unique data-id to each row so we can reference easier
var uniqueId = 0;
function createUniqueId(){
return ++uniqueId+'_tr';
}
Loop all tbody>tr and assign a unique data-id
$('table tbody tr').each(function(index,tr){
$(tr).attr('data-id',createUniqueId());
});
On each keyup of the #order_search element we:
Grab the value
Clear any extra spaces
Split with space and create an array of each word
Loop this array and inside this array loop each row and in every row loop each column (like you did before)
If we have a match, save this row (instead of not found a match) in the rows_found array
6.Loop all rows and the rows that are not in the rows_found array hide them
$("#order_search").keyup(function () {
//In every keyup restore all rows
$("table tbody tr").each(function(index, tr){
$(tr).css({'display':'table-row'})
});
var value = $(this).val().toLowerCase().trim();
//remove extra spaces
value = value.replace(/\s+/g,' ');
//Split the search field value by ' ' space and create an array which we will loop
var value_ar = value.split(' ');
var rows_found = [];
for(var v=0;v<value_ar.length;v++){
var cvalue = value_ar[v];
$("table tbody tr").each(function (index,tr) {
$(tr).find("td").each(function (indextd, td) {
var id = $(td).text().toLowerCase().trim();
var found = (id.indexOf(cvalue) != -1)?true:false;
if(found){
//save a reference of all tr data-ids that found a match
rows_found.push($(this).parent().attr('data-id'));
}
});
});
}//end for
//Loop of rows and hide the one that were not found
$("table tbody tr").each(function(index, tr){
var myid = $(tr).attr('data-id');
if($.inArray(myid,rows_found)<0){
$(tr).css({'display':'none'});
}
});
});