Javascript Search Exact Results Not Part Of -
i trying use javascript search below website wondering if there way return exact term searched for, if type part of word returns table row also.
ie. seach "heath" returns same result searching heathesh", there simple workaround?
script: http://heathesh.com/post/2010/05/06/filtering-or-searching-an-html-table-using-javascript.aspx
example: http://heathesh.com/code/javascript/tablesearch/
<table border="1" cellpadding="0" cellspacing="0"> <tr> <th>id</th> <th>first name</th> <th>surname</th> <th>website</th> </tr> <tbody id="data"> <tr> <td>1</td> <td>heathesh</td> <td>bhandari</td> <td><a href="http://heathesh.com">http://heathesh.com</a></td> </tr> <tr> <td>2</td> <td>candice</td> <td>david</td> <td><a href="http://candicedavid.com">http://candicedavid.com</a></td> </tr> </tbody> </table> //define table search object, can implement both functions , properties window.tablesearch = {}; //initialize search, setup current object tablesearch.init = function() { //define properties want on tablesearch object this.rows = document.getelementbyid('data').getelementsbytagname('tr'); this.rowslength = tablesearch.rows.length; this.rowstext = []; //loop through table , add data table search object (var = 0; < tablesearch.rowslength; i++) { this.rowstext[i] = (tablesearch.rows[i].innertext) ? tablesearch.rows[i].innertext.touppercase() : tablesearch.rows[i].textcontent.touppercase(); } } next create actual javascript function run search so:
//onlys shows relevant rows determined search string tablesearch.runsearch = function() { //get search term this.term = document.getelementbyid('textboxsearch').value.touppercase(); //loop through rows , hide rows not match search query (var = 0, row; row = this.rows[i], rowtext = this.rowstext[i]; i++) { row.style.display = ((rowtext.indexof(this.term) != -1) || this.term === '') ? '' : 'none'; } } //handles enter key being pressed tablesearch.search = function(e) { //checks if user pressed enter key, , if did run search var keycode; if (window.event) { keycode = window.event.keycode; } else if (e) { keycode = e.which; } else { return false; } if (keycode == 13) { tablesearch.runsearch(); } else { return false; } } <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <input type="text" size="30" maxlength="1000" value="" id="textboxsearch" onkeyup="tablesearch.search(event);" /> <input type="button" value="search" onclick="tablesearch.runsearch();" /> </td> </tr> </tbody> </table>
you matching rowtext.indexof() in code below, return row if term found anywhere in string.
for (var = 0, row; row = this.rows[i], rowtext = this.rowstext[i]; i++) { row.style.display = ((rowtext.indexof(this.term) != -1) || this.term === '') ? '' : 'none'; } to exact matches, change rowtext.indexof(this.term) != -1 rowtext.touppercase() === this.term.touppercase(). .touppercase() converts both strings uppercase before comparing make search case insensitive.
for (var = 0, row; row = this.rows[i], rowtext = this.rowstext[i]; i++) { row.style.display = ((rowtext.touppercase() === this.term.touppercase()) || this.term === '') ? '' : 'none'; }
Comments
Post a Comment