Hello everyone,
I made a little search-function with javascript to look for a string in a table: There are "tr"s that simply contain a number as an id and there are "tr"s that contain "childNode+idOfParentNode" as an id (e.g.:
<tr id="1"> ... </tr>
<tr id="childNode1"> ... </tr>
Now I want to look through the table, see if a giving string or part of it matches the content of the parent-"tr". If that is not the case I want the parent-"tr" and its childNode-"tr"s to be hidden (or collapsed). And I want them being shown if the string or part of it matches. Here is my function:
// inputFieldId := Id of the inputField that contains the search-String
// tableId := Id of the table to be searched
function searchTable( inputFieldId, tableId ){
var inputField = document.getElementById( inputFieldId );
var input = inputField.value.toUpperCase();
var countRows = jQuery( '#' + tableId + ' tr' ).length;
jQuery('#loader').css( "visibility", "visible" );
var hideChildren = false;
var childId = -1;
var parentId = -1;
for( var i = 1; i <= countRows; i++ ){
var trsId = jQuery('#' + tableId + ' tr:nth-child('+i+')' ).attr('id');
// I am only looking for <tr> that are not "childnodes"
if( trsId.indexOf( "childNode") == -1 ){
var firstTd = jQuery('#' + tableId + ' tr:nth-child('+i+') td:nth-child(1)' );
var firstTdValue = firstTd.text();
if( firstTdValue.indexOf( input ) == -1 ){
hideChildren = true;
childId = trsId;
parentId = i;
jQuery('#' + tableId + ' tr:nth-child('+i+')' ).children('td').css("visibility", "collapse");
jQuery('#' + tableId + ' tr:nth-child('+i+')' ).css("visibility", "collapse");
}
else{
hideChildren = false;
childId = trsId;
parentId = i;
jQuery('#' + tableId + ' tr:nth-child('+i+')' ).children('td').css("visibility", "visible");
jQuery('#' + tableId + ' tr:nth-child('+i+')' ).css("visibility", "visible");
}
}
else{
childNodeId = "childNode"+childId;
if( hideChildren && trsId == childNodeId && parentId > -1 ){
jQuery('#' + tableId + ' tr:nth-child('+i+')' ).children('td').css("visibility", "collapse");
jQuery('#' + tableId + ' tr:nth-child('+i+')' ).css("visibility", "collapse");
}
else{
jQuery('#' + tableId + ' tr:nth-child('+i+')' ).children('td').css("visibility", "visible");
jQuery('#' + tableId + ' tr:nth-child('+i+')' ).css("visibility", "visible");
}
}
}
jQuery('#loader').css( "visibility", "hidden" );
}
Seriously, this works fine, but it takes FOREVER!!! especially if it is a larger table, so I was wondering if someone saw a way to make my function faster and more efficient.
Thnx in advance :)
=========================================================================
EDIT: I made it work ... it now looks like this and works wonderfully :)
function searchTable( inputFieldId, tableId ){
jQuery('#loader').show();
var input = jQuery('#'+inputFieldId).val().toUpperCase();
var parentId = -1
jQuery('#' + tableId + ' tr').each( function(i) {
var thiss = jQuery(this);
var showP = false;
var showC = false;
if (thiss.attr('id').indexOf('child') < 0) { // parent
parentId = thiss.attr('id');
showP = !(thiss.find('td:first').text().indexOf( input ) < 0);
thiss.toggle( showP );
}
else{ // childNode
var childId = "childNode"+parentId;
var parent = jQuery('#'+tableId+' tr#'+parentId+':visible').length;
showC = !(thiss.attr('id') == childId && parent < 1);
thiss.toggle( showC );
}
});
jQuery('#loader').css( "visibility", "hidden" );
}
Thank you :)