2010-08-23 41 views
3

以下函數需要至少3秒才能運行(在500個錶行上)。是否有可能使此功能更快?功能很慢,有沒有更快的方法? (使用jQuery 1.4.2)

function prepareTable() { 
    var groupIndex = 0; 
    $("#row tbody tr").each(function(index) { 
    // each row gets a unique id 
    // remove default css styles for table rows 
    // read out hidden value, that stores if row is a group 
    var group = $(this).attr('id', 'node-'+index).removeClass("odd event").find('td :hidden').attr('value'); 
    // if it is a group, add special styles to row and remember row index 
    if (group == 'true') { 
     groupIndex = index; 
     $(this).addClass('odd').find("td:first") 
      .mouseenter(function() { 
       $(this).parent().addClass("swGroupLink"); 
      }) 
      .mouseleave(function() { 
       $(this).parent().removeClass("swGroupLink"); 
     }); 
    } else { 
     // make all following rows to children of the previous group found 
     $(this).addClass('even child-of-node-' + groupIndex); 
    } 
    }); 
} 
+0

全部500個錶行是可見的,還是你使用分頁? – 2010-08-23 07:00:55

+0

我也在使用分頁。 thx的提示... – Steven 2010-08-23 07:18:57

+0

然後你可以做只處理表的可見部分。 – 2010-08-23 07:29:37

回答

6

我建議兩方面的改進:

  • 緩存DOM References
  • 工作在你的餐桌離線

function prepareTable() { 
    var groupIndex = 0; 
    var $mytable = $('#row'), 
     $parent = $mytable.parent(); 

    $mytable = $mytable.detach(); 

    $mytable.find('tr').each(function(index) { 
     var $this = $(this); 
     var group = $this.attr('id', 'node-'+index).removeClass("odd event").find('td :hidden').attr('value'); 
// if it is a group, add special styles to row and remember row index 
    if (group == 'true') { 
     groupIndex = index; 
     $this.addClass('odd').find("td:first") 
      .mouseenter(function() { 
       $this.parent().addClass("swGroupLink"); 
      }) 
      .mouseleave(function() { 
       $this.parent().removeClass("swGroupLink"); 
     }); 
    } else { 
     // make all following rows to children of the previous group found 
     $this.addClass('even child-of-node-' + groupIndex); 
    } 
    }); 

    $parent.append($mytable); 
} 

我添加了一個變量$this,它將$(this)緩存在您的.each()循環中。我還加入了$mytable$parent$mytable存儲#row元素,$parent存儲#row的父節點。這是因爲我從DOM中刪除了整個元素,完成這些工作並將其重新附加到父級。

測試環境:http://www.jsfiddle.net/2C6fB/4/

如果仍然太慢,你還有其他選擇這裏。首先,看看你是否可以將循環分成更小的部分。您可以使用asychronous回調來優化,例如setTimeout。這可能是一個棘手的業務,我需要更詳細地瞭解您的代碼,但通常情況下,您可能只想將整個循環包裝到單個setTimeout()函數中。示例 - >http://www.jsfiddle.net/2C6fB/5/

這可確保瀏覽器在操作時不會「掛起」。但是,當然這需要更長的時間才能完成整個任務。

+0

您的優化對我來說聽起來非常好。不幸的是,該功能現在慢了大約20%:-( (使用Firebug和Google Chrome開發者工具進行測試) – Steven 2010-08-23 07:22:19

+0

@Steven:這聽起來幾乎不可能,我正在建立一個快速測試環境。 – jAndy 2010-08-23 07:32:23

+0

@jAndy:非常感謝。我也很困惑...... – Steven 2010-08-23 07:41:32

-1

我猜測發現是所有時間都消失的。

你能不能找到它的選擇器嗎?什麼是HTML?

+0

爲所有500行找到需要80ms – Steven 2010-08-23 07:40:10

1

您可以將帶鼠標的mouseenter和mouseleave帶到實時事件的外面,這樣它就不會用prepareTable函數執行,並且您可以將它放入文檔就緒函數中。

$("#row tbody tr td.trueInPrepareTable") 
    .live("mouseenter", function(event){  
       $(this).parent().addClass("swGroupLink");  
    }).live("mouseleave", function(event){  
       $(this).parent().removeClass("swGroupLink");  
    }); 

不是從隱藏字段獲取組值,而是將此值放在rel,rev或title屬性中。

function prepareTableEdit() { 
       var groupIndex = 0; 
       $("#row tbody tr").each(function(index) { 
        groupIndex = index; 
        $(this).attr('id', 'node-'+ groupIndex).removeClass("odd even"); 
        if($(this).attr('rel') == 'true') 
        {       
         $(this).addClass('odd').find("td:first").addClass('trueInPrepareTable');      } 
        else 
        { 
         $(this).addClass('even child-of-node-' + groupIndex).find("td:first").removeClass('trueInPrepareTable'); 
        } 
       }); 

} 

退房http://www.jsfiddle.net/raBGq/

+0

不錯的主意,但幾乎相同的執行時間(-100至-200毫秒) – Steven 2010-08-23 07:31:15

+0

檢查出http://www.jsfiddle.net/raBGq/。它運行了160毫秒,比你的要快得多。您需要打開螢幕控制檯才能查看結果。 – 2010-08-24 06:22:01

+0

你的新版本忽略了「子節點 - 」 – Steven 2010-08-24 08:35:42

相關問題