Searching Html Table
I have created a table with HTML and want to integrate a search box. How do i do that? Can you recommend a good jQuery plugin or better a complete tutorial?
Solution 1:
A quick and dirty approach, using jQuery:
$(document).ready(
function(){
$('#searchbox').keyup(
function(){
var searchText = $(this).val();
if (searchText.length > 0){
$('td:contains(' + searchText +')')
.css('background-color','#f00');
$('td:not(:contains('+searchText+'))')
.css('background-color','#fff');
}
});
});
With the following (x)html:
<table><thead><tr><tdcolspan="2"><labelfor="searchbox">Search:</label><inputtype="text"id="searchbox" /></td></tr></thead><tbody><tr><td>Something</td><td>More text</td></tr><tr><td>Lorem ipsum</td><td>blah?</td></tr></tbody></table>
Edited to use addClass()
/removeClass()
, in place of setting the css in the jQuery itself:
$(document).ready(
function(){
$('#searchbox').keyup(
function(){
var searchText = $(this).val();
if (searchText.length > 0){
$('td:contains(' + searchText +')')
.addClass('searchResult');
$('td:not(:contains('+searchText+'))')
.removeClass('searchResult');
}
elseif (searchText.length == 0) {
$('td.searchResult')
.removeClass('searchResult');
}
});
});
To fade out the table cells that don't contain the search result you can use the following:
jQuery:
$(document).ready(
function(){
$('#searchbox').keyup(
function(){
var searchText = $(this).val();
if (searchText.length > 0) {
$('tbody td:contains('+searchText+')')
.addClass('searchResult');
$('.searchResult')
.not(':contains('+searchText+')')
.removeClass('searchResult');
$('tbody td')
.not(':contains('+searchText+')')
.addClass('faded');
$('.faded:contains('+searchText+')')
.removeClass('faded');
}
elseif (searchText.length == 0) {
$('.searchResult').removeClass('searchResult');
$('.faded').removeClass('faded');
}
});
});
css:
td.faded {
opacity: 0.2;
}
Solution 2:
There are great answers. But this guy did what i Really wanted. it's slighly different from previous answers
HTML
<labelfor="kwd_search">Search:</label><inputtype="text"id="kwd_search"value=""/><tableid="my-table"border="1"style="border-collapse:collapse"><thead><tr><th>Name</th><th>Sports</th><th>Country</th></tr></thead><tbody><tr><td>Sachin Tendulkar</td><td>Cricket</td><td>India</td></tr><tr><td>Tiger Woods</td><td>Golf</td><td>USA</td></tr><tr><td>Maria Sharapova</td><td>Tennis</td><td>Russia</td></tr></tbody></table>
Javascript (Jquery)
// 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 blankif( $(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;
}
});
Live Demo: http://blogs.digitss.com/demo/jquery-table-search.html
Solution 3:
searching tables is simple:
$('#table1 tr td').each(function(){
if ( this.innerHTML.indexOf(searchKeyword) > -1 )
this.style.color = 'yellow'//found within this td so highlight it
});
Solution 4:
Thanks David Thomas
Good Solution. Following makes it perfect.
$(document).ready(
function(){
$('#searchbox').keyup(
function(){
var searchText = $(this).val();
if (searchText.length > 0) {
$('tbody td:contains('+searchText+')')
.addClass('searchResult');
$('.searchResult')
.not(':contains('+searchText+')')
.removeClass('searchResult');
$('tbody td')
.not(':contains('+searchText+')')
.addClass('faded');
$('.faded:contains('+searchText+')')
.removeClass('faded');
$('.faded').hide();
$('.searchResult').show();
}
elseif (searchText.length == 0) {
$('.searchResult').removeClass('searchResult');
$('.faded').removeClass('faded');
$('td').show();
}
});
});
Post a Comment for "Searching Html Table"