Ok, this is what i came up with. It's inspired on someone else's code
but modified to run faster.
If you're wondering why a "setInterval" instead of a link to a "onKeyUp"
event, it's because it's still kinda slow for big (1000+ rows) table
which makes InternetExplorer freeze during keystrokes... very anoying.
It must be placed after the datagrid declaration since it refers to it.
--> If anyone could see a way to make it faster, I would apreciate the
help. It can still be a little laggy on huge tables.
Luis Ferrao
PS: Jeff, i'll have a look but it sounds nice and encapsulated the way
you put it
*******************************************
***************** CODE ********************
*******************************************
var filter = "";
var tableId = "DataGrid";
var rowHeight = 16;
var table = document.getElementById(tableId);
var rows = table.getElementsByTagName('tr');
// the following must be called at the end of declarations
var listnerInterval = setInterval("applyFilter(filter)", 1000);
function applyFilter(pattern)
{
counter = 0 // visible rows counter
// iterate through and search the array (<tr>'s)
outer_loop:
for (i=0; i < rows.length; i++)
{
if (pattern == "")
{
rows[i].className='visible'
counter++;
continue outer_loop
}
td = rows[i].getElementsByTagName('td')
// iterate through <td>'s
for (j = 0; j < td.length; j++)
{
nodeVal = td[j].firstChild.nodeValue; // gets us the #text value
of the first td tag
try {
if (nodeVal.toLowerCase().indexOf(pattern.toLowerCase ()) != -1
|| pattern == '')
{
// match found for this row (or pattern is ''), so set as
visible
// rows[i].setAttribute('class', 'visible');
rows[i].className='visible'
// increment visible rows counter but only once per row
if (j == 0) counter++
continue outer_loop
}
else {
// match not found for this row, so set as hidden
//rows[i].setAttribute('class', 'hidden');
rows[i].className='hidden'
}
} catch (ex) {}
}
}
// fix table height at the end of the loop
table.style.height = (counter * rowHeight) + "px"
}
*******************************************
***************** EOC *********************
*******************************************
Note: The search field onkeyup event must modify the filter var
obviously. But at least this way the app doesn't have to go through the
whole function between each keystroke.
*** Sent via Developersdex
http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!