2017-03-06 37 views
0

我目前有一個應用程序,當點擊一個按鈕時,它會發送一個$ http請求並返回一些信息。然後用所述數據更新變量rowscolumnsng-repeat繼續循環瀏覽我的所有數據。Angular JS在使用ng-repeat遍歷海量數據集時凍結DOM

當我增加數據超過1000個對象時,它完全鎖定了DOM。我如何優化我的代碼,使其不鎖定DOM 有史以來?這一點非常重要,因爲我的用戶需要能夠在數據迭代完成時在頁面上執行操作。我需要假設的解決方案來處理多達10000行。

我的HTML ...

<table> 
    <tr> 
     <th ng-repeat="column in columns | orderBy:columnToSortBy:reverse"> 
      <h6 ng-bind="column.name" ng-click='sortBy(column.name)'></h6> 
      <img src='/static/images/asc.gif' ng-show="orderByAsc"> 
      <img src='/static/images/desc.gif' ng-show="orderByDesc"> 
      <img src='/static/images/bg.gif' ng-show="orderByNothing"> 
     </th> 
    </tr> 
    <tr ng-repeat="row in rows"> 
     <td ng-repeat="(key, value) in row" ng-bind="value"></td> 
    </tr> 
</table> 

這裏是控制器,其中HTML被附加正在創建,追加,並且$compile「編...

mainQueryModule.controller("newQueryController", function($scope, $http, $compile){ 
var results = {columns:[{"type": "STRING", "name": "nk"}, {"type": "STRING", "name": "Bn"}, {"type": "STRING", "name": "Sk"},{"type": "STRING", "name": "ank"}, {"type": "STRING", "name": "Bnk"}, {"type": "STRING", "name": "Skk"}, {"type": "STRING", "name": "Name"}, {"type": "STRING", "name": "Phone"}, {"type": "STRING", "name": "Age"}, {"type": "STRING", "name": "Tank"}, {"type": "STRING", "name": "Bank"}, {"type": "STRING", "name": "Skunk"}], rows:[]}; 
for(index = 0; index < 1000; index++){ 
     results['rows'].push({nk: "select * from faker 100000000000", Bn: 35, Sk: 567, ank: "ank", Bnk: "Bnk", Skk: "Skk", Name: "Erik", Phone: 100000+index, Age: 29+index, Bank: "Big"+index, Tank: 16+index, Skunk: "123"+index}) 
} 

$scope.runQuery = (client_id) => { 
     // $http.post("fakeurl/", {test:123}).then(function(response){ 
     //  console.log(response) 
      $scope.columns = results["columns"]; 
      $scope.columnToSortBy = null; 
      $scope.rows = results["rows"]; 
      $scope.reverse = false; 
      $scope.orderByAsc = false; 
      $scope.orderByDesc = false; 
      $scope.orderByNothing = true; 
     // }) 
} 
}) 

根據@Gaurav建議反映變化的更新問題。

+0

使用web worker。並在後臺運行 –

+0

爲什麼你需要1000行?使用分頁,用戶無法處理該行數。這將編譯更少的信息,並且DOM也不會凍結 –

+0

也更好地使用指令,並且不會從控制器處理dom。讓控制器只需使用$ http請求數據並創建結果行,並讓您的模板自己生成html –

回答

0

你需要做它在angularjs方式如:你的HTML應該是這樣的

<table class='results'> 
    <tr class='results-column-tr'> 
    <th ng-repeat="column in columns"><h6 class='results-column-titles' ng-click='sortBy(column.name)">{{column.name}}</h6><img ng-show='orderByDesc' src='/static/images/asc.gif'><img ng-show='orderByAsc' src='/static/images/desc.gif'><img ng-show='orderByNothing' src='/static/images/bg.gif'> 
    </th> 
    </tr> 
    <tr ng-repeat='row in rows | orderBy:columnToSortBy:reverse'> 
    <td ng-repeat="column in columns" class='results-data' ng-bind='row[column.name]'> 
    </td> 
    </tr> 
</table> 

和控制器

mainQueryModule.controller("newQueryController", function($scope, $http){ 
    var results = {columns:[{"type": "STRING", "name": "nk"}, {"type": "STRING", "name": "Bn"}, {"type": "STRING", "name": "Sk"},{"type": "STRING", "name": "ank"}, {"type": "STRING", "name": "Bnk"}, {"type": "STRING", "name": "Skk"}, {"type": "STRING", "name": "Name"}, {"type": "STRING", "name": "Phone"}, {"type": "STRING", "name": "Age"}, {"type": "STRING", "name": "Tank"}, {"type": "STRING", "name": "Bank"}, {"type": "STRING", "name": "Skunk"}], rows:[]}; 
    for(index = 0; index < 1000; index++){ 
    results['rows'].push({nk: "select * fotm ;ldfg;lkjdopiusergknds;lkbjs;dlfkjoisdug;lkjsdfg;lkjsdfgl;kjsdfg;lkjsdfgl;iusoe[rtuoipejl;ksdjsdjgoupsdgoupieopiutwjsgoji;duiejlds;fgjsdjgdfgsjsdgsdfgjsdgkl", Bn: 35, Sk: 567, ank: "ank", Bnk: "Bnk", Skk: "Skk", Name: "Erik", Phone: 100000+index, Age: 29+index, Bank: "Big"+index, Tank: 16+index, Skunk: "123"+index}) 
    } 
    $scope.columns = results["columns"]; 
    $scope.rows = results["rows"]; 
}); 

不要在jQuery的風格做到這一點。

+0

我同意。我應該以角度js的方式來做。這是我的問題。如果我這樣做,我需要它是動態的,所以當$ scope.columns和$ scope.rows改變時,我仍然可以使用orderBy。這會爲此工作嗎? –

+0

Surly其對動態內容和訂單的工作將始終保持不變。 – Gaurav

+0

嘿。所以我實施了你的解決方案它是動態的。 orderBy確實有效。但是dom仍然會凍結更大的數據集。任何方式使這個更快?將ng-repeat放入指令會讓它更快嗎? –