I want to do a live search through the table rows, using jQuery, the "live" word is the key, because I want to type the keywords in the text input, on the same site and I'd like jQuery to automaticaly sort (or remove those who doesn't match the search query) the table rows.
Here is my HTML:
<table>
<tr><th>Unique ID</th><th>Random ID</th></tr>
<tr><td>214215</td><td>442</td></tr>
<tr><td>1252512</td><td>556</td></tr>
<tr><td>2114</td><td>4666</td></tr>
<tr><td>3245466</td><td>334</td></tr>
<tr><td>24111</td><td>54364</td></tr>
</table>
And if I would fe. search by the Unique ID
, it should show the only rows that starts from the certain number for the Unique ID. Fe. if I would type '2' in the search input box, the following rows should stay, as they begin with 2
:
<table>
<tr><th>Unique ID</th><th>Random ID</th></tr>
<tr><td>214215</td><td>442</td></tr>
<tr><td>2114</td><td>4666</td></tr>
<tr><td>24111</td><td>54364</td></tr>
</table>
If I would type 24
, then there should be only one row visible as it begins from the 24
:
<table>
<tr><th>Unique ID</th><th>Random ID</th></tr>
<tr><td>24111</td><td>54364</td></tr>
</table>
If you guys could give me some tips on how to do something like this I would appreciate it so much.
Thank you.
Here's a version that searches both columns.
demo: http://jsfiddle.net/rFGWZ/369/
Here's how I live search a html table:
<input type='text' onkeyup="filterTo(this.value, 'myTable')" placeholder='Search...'>
<table id='myTable'>...</table>
Below JS function you can use to filter the row based on some specified columns see searchColumn array. It is taken from w3 school and little bit customised to search and filter on the given list of column.
HTML Structure
Using yckart's answer, I made it to search for the whole table - all td's.
I'm not sure how efficient this is but this works:
DEMO - Live search on table
I did add some simplistic highlighting logic which you or future users might find handy.
One of the ways to add some basic highlighting is to wrap
em
tags around the matched text and using CSS apply a yellow background to the matched text i.e: (em{ background-color: yellow }
), similar to this:Demo - applying some simple highlighting
Old question but i find out how to do it faster. For my example: i have about 10k data in my table so i need some fast search machine.
Here is what i did:
Hope it helps somebody.