Reputation: 41
The blow is a basic filter search for tables. Currently it filters and searches the entire table (thead & tbody). Is there any way to make this script search just the tbody? I want to exclude the thead from the filtered results and I've tried appending the "id="kwd_search"" to just the instead of the tag, but it makes the script not function. Another I would like it not to do is scale the table rows smaller when it finds results.
Any help is greatly appreciated and thanks for reading. Here is a live demo of what I have. I'm really slow with this stuff, so I would really be nice if I could get a live example back if it isn't much trouble.
// When document is ready: this gets fired before body onload <img src='http://blogs.digitss.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
$(document).ready(function(){
// Write on keyup event of keyword input element
$("#kwd_search").keyup(function(){
// When value of the input is not blank
if( $(this).val() != "")
{
// Show only matching TR, hide rest of them
$("#my-table tbody>tr").hide();
$("#my-table td:contains-ci('" + $(this).val() + "')").parent("tr").show();
}
else
{
// When there is no input or clean again, show everything back
$("#my-table tbody>tr").show();
}
});
});
// jQuery expression for case-insensitive filter
$.extend($.expr[":"],
{
"contains-ci": function(elem, i, match, array)
{
return (elem.textContent || elem.innerText || $(elem).text() || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
}
});
Upvotes: 3
Views: 6011
Reputation: 171690
I didn't feel like debugging the contains-ci
approach. Can replace it with filter()
// When document is ready: this gets fired before body onload <img src='http://blogs.digitss.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
$(document).ready(function() {
/* cache td elements to improve search performance*/
var $rows=$("#my-table tbody>tr"), $cells=$rows.children();
// Write on keyup event of keyword input element
$("#kwd_search").keyup(function() {
var term = $(this).val()
// When value of the input is not blank
if(term != "") {
// Show only matching TR, hide rest of them
$rows.hide();
$cells.filter(function() {
return $(this).text().toLowerCase().indexOf(term) > -1;
}).parent("tr").show();
} else {
// When there is no input or clean again, show everything back
$rows.show();
}
});
});
DEMO: http://jsfiddle.net/aPLLF/2/
Upvotes: 4