2015-11-24 28 views
0

如何使用函數計算記錄時,如何在angularjs中對錶進行排序?我應該如何在控制器中使用排序功能?角JS複雜的記錄行排序

例如,我的兩個記錄:

<td>{{object.lastName}}</td> 
<td>{{getValue(objecct1, object2, 'string')}}</td> 

和頭我有這樣的:

<th ng-click="order('string')">String<span ng-show="predicate === 'string'"

我可以排序基於lastName的,因爲它沒有嵌套,但我無法根據來自getter函數的記錄進行排序。

我認爲我的問題是我認爲無法找到相應數據進行排序的謂詞。

此外,在控制器我有這行:

$scope.predicate = 'lastName'; 
$scope.reverse = false; 

$scope.order = function(predicate) { 
    $scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false; 
    $scope.predicate = predicate; 
}; 

什麼建議嗎?

+0

我剛剛更新了我的回答,我認爲那是什麼你正在尋找 – masterpreenz

回答

0

我只是想知道你使用ng-repeat來顯示你的嵌套數據?如果是,請嘗試檢出my answer here。我對你的問題的答案是幾乎相同與

編輯:

剛剛嘗試看看我的答案在我掛在這裏後,我會告訴你我是如何存儲我的數據,成爲整個訪問應用程序使用Service

.service("DataProvider", function() { 
    var _data = []; 
    var obj = { 
     update: function() { 
      // Do an $http to update your data 
     }, 
     getCachedData: function() { 
      return _data; 
     } 
    }; 

    return obj; 
}); 

所以你可以很容易地通過注入您創建的服務讓您的數據在你的應用程序在controllerdirective或其他service稱爲DataProvider

.controller("YourControllerName", function ($scope, DataProvider) { 
    // Here I will call getCached data every time this controller is called 
    $scope.myData = DataProvider.getCachedData(); 

    // And your sorting function would pretty much be in here 
    $scope.order = function() { 
     // Do an sorting algo here 
     var sorted = []; 
     angular.forEach(sorted, function() { 
      // Do your fancy sorting algo 
     }); 

     $scope.myData = sorted; 
    }; 
}); 
+0

好,但你能解釋你用什麼函數來存儲? – Mohammad

+0

我剛剛更新了我的答案我認爲這就是你要找的 – masterpreenz