Yes, you're right, it should be a reusable code. It's known as DRY ( Do not Repeat Yourself )
So one way of doing this is to create a function which handles the search functionality
Example :
function setSearch(inputEle, rowEle){
$(inputEle).keyup(function(){
search_table(rowEle, $(this).val());
});
}
function search_table(rowEle, value){
$(rowEle).each(function(){
var found = 'false';
$(this).each(function(){
if($(this).text().toLowerCase().indexOf(value.toLowerCase()) >= 0)
{
found = 'true';
}
});
if(found == 'true')
{
$(this).show();
}
else
{
$(this).hide();
}
});
}
Now you can set search functionality by calling setSearch for all the tables like :
setSearch('#search', '#table_body tr');
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…