快速制作Javascript-search-function

时间:2009-08-05 12:05:21

标签: javascript jquery search

我使用javascript创建了一个小搜索函数来查找表中的字符串: 有些“tr”只包含一个数字作为id,并且“tr”包含“childNode + idOfParentNode”作为id(例如:

<tr id="1"> ... </tr>
<tr id="childNode1"> ... </tr>

现在我想查看表格,查看给定字符串或其中的一部分是否与父级“tr”的内容相匹配。如果不是这种情况,我希望隐藏(或折叠)父 - “tr”及其childNode-“tr”。如果字符串或其中的一部分匹配,我希望它们被显示出来。这是我的功能:

// 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" );
}

说真的,这个工作正常,但它需要永远!特别是如果它是一个更大的桌子,所以我想知道是否有人看到了一种方法,使我的功能更快,更有效。

Thnx提前:)

=============================================== ==========================

编辑:我让它发挥作用......它现在看起来像这样并且效果非常好:)

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" );
}

谢谢:)

3 个答案:

答案 0 :(得分:2)

1)缓存您多次创建的选择器。然后使用从那时开始的变量。

var $rows = jQuery('#' + tableId + ' tr:nth-child('+i+')' );

$rows.children()...

2)为了得到直接的孩子,你可以使用'&gt;'在你的选择器

 var $rows = jQuery('#' + tableId + '>tr:nth-child('+i+')' );

答案 1 :(得分:2)

如果父母(和孩子)有类别识别它们会更容易,但如果需要,你可以使用id。我使用的是$而不是jQuery,但如果需要,你可以改回来。

// inputFieldId := Id of the inputField that contains the search-String
// tableId := Id of the table to be searched
function searchTable( inputFieldId, tableId ){
    var input = $('#' + inputFieldId).text().ToUpperCase();

    $('#loader').show();

    $('#' + tableId + ' tr').each( function(i) {
         var $this = $(this);
         var show = false;
         //  if ($this.hasClass('parent')) {  // would be nice
         if ($this.attr('id').indexOf('child') < 0) { // parent
             // note that text() here is the combined texts of all tds
             // adjust with :first if you really want to check only the first
             show = !($this.find('td').text().indexOf( input ) < 0);
         }
         $this.toggle( show );
    });

    $('#loader').hide();
}

答案 2 :(得分:1)

var rowsCache = null;
function searchTable( inputFieldId, tableId ){

    var input = String(jQuery("#inputFieldId").val()).toUpperCase();

    if (rowsCache==null)
        rowsCache = jQuery( '#' + tableId + ' tr');

    jQuery('#loader').css( "visibility", "visible" );

    //if there are many rows is faster --
    //for(var i = (countRows-1); i >= 0; i--) {

    jQuery(rowsCache).each(function() {
        if ((jQuery(this).html().indexOf(input)!=-1)
        {
            ...
        }
    });
    jQuery('#loader').css( "visibility", "hidden" );
}
相关问题