2016-08-08 93 views
3

我需要爲我所用排序依據過濾表中的數據進行排序,並正從最後一排上面 plunker例如分類將更加explanable表使用排序依據排序工作不正常

DEMO

HTML:

<table class="friends"> 
    <thead> 
    <tr class='table-head'> 
     <th scope="col">Candidate Name</th> 
     <th scope="col" ng-repeat="a in [1,2,3,4]" ng-click="sortBy('candidateData.rating')"><a style="cursor:pointer">Round{{a}}</a></th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr ng-repeat="candidate in aCandidateDetails| orderBy:propertyName:reverse"> 
     <td> 
     <div>{{candidate.name}}</div> 
     </td> 
     <td ng-repeat="candidateData in candidate.ratings"> 
     <div ng-if="candidateData.rating"> 
      {{candidateData.rating}} 
     </div> 
     <span ng-if="!candidateData.rating"> - NA - </span> </td> 
     <td data-title="Status"> 
     <div>{{candidate.interviewStatus}}</div> 
     </td> 
     <td><a href="" ng-model='candidate' ng-click="fnCandidateFeedbackDetails(candidate.uniqueId,candidate._id)"><i class="fa fa-info-circle" aria-hidden="true"></i></a></td> 
    </tr> 
    </tbody> 
</table> 

JS:

$scope.propertyName = 'name'; 
    $scope.reverse = true; 
    $scope.sortBy = function(propertyName) { 
    $scope.reverse = ($scope.propertyName === propertyName) ? !$scope.reverse : false; 
    $scope.propertyName = propertyName; 
    }; 

JSON:

$scope.aCandidateDetails = [{ 
    name: "a", 
    ratings: [{ 
     round: 1, 
     rating: 3, 
     feedback: "something", 
     interviewer: "abc", 
     roundStatus: "selected" 
     }, 

     { 
     round: 2, 
     rating: 5, 
     feedback: "something", 
     interviewer: "abc", 
     roundStatus: "selected" 
     }, { 
     round: 3, 
     rating: 4, 
     feedback: "something", 
     interviewer: "abc", 
     roundStatus: "selected" 
     }, { 
     round: 4, 
     rating: 1, 
     feedback: "something", 
     interviewer: "abc", 
     roundStatus: "selected" 
     } 
    ] 
    }, { 
    name: "b", 
    ratings: [{ 
     round: 1, 
     rating: 5, 
     feedback: "something", 
     interviewer: "abc", 
     roundStatus: "selected" 
     }, 

     { 
     round: 2, 
     rating: 4, 
     feedback: "something", 
     interviewer: "abc", 
     roundStatus: "selected" 
     }, { 
     round: 3, 
     rating: 3, 
     feedback: "something", 
     interviewer: "abc", 
     roundStatus: "selected" 
     }, { 
     round: 4, 
     rating: 2, 
     feedback: "something", 
     interviewer: "abc", 
     roundStatus: "selected" 
     } 
    ] 
    },{ 
    name:"c", 
    ratings: [{ 
     round: 1, 
     rating: 1, 
     feedback: "something", 
     interviewer: "abc", 
     roundStatus: "selected" 
     }, 

     { 
     round: 2, 
     rating: 1, 
     feedback: "something", 
     interviewer: "abc", 
     roundStatus: "selected" 
     }, { 
     round: 3, 
     rating: 1, 
     feedback: "something", 
     interviewer: "abc", 
     roundStatus: "selected" 
     }, { 
     round: 4, 
     rating: 1, 
     feedback: "something", 
     interviewer: "abc", 
     roundStatus: "selected" 
     }] 
    }] 

如果我們上一輪的點擊,那麼它應該來挑選基於等級按降序排列未發生

任何幫助表示讚賞

回答

1

你將不得不編寫自己的比較和與orderBy一起使用。像這樣的東西。

$scope.ratingComparator = function(v1, v2) { 
    if (v1.type === 'object') { 
     var obj1 = $scope.aCandidateDetails[v1.index]; 
     var obj2 = $scope.aCandidateDetails[v2.index]; 
     return (obj1.ratings[$scope.sortRound].rating < obj2.ratings[$scope.sortRound].rating)? -1: 1; 
    } else { 
     return 1; 
    } 
    } 

而在你的HTML,

<tr ng-repeat="candidate in aCandidateDetails| orderBy:propertyName:reverse:ratingComparator"> 

同時,由於數據的結構,你必須通過地方存放它的範圍跟蹤輪數。這是因爲每輪都沒有單獨的字段。這是通過輪數實現對sortBy

<th scope="col" ng-repeat="a in [1,2,3,4]" ng-click="sortBy('ratings', a)"><a style="cursor:pointer">Round{{a}}</a></th> 

這裏是更新 Plnkr

這裏假定評分數組按照開始的整數排序。如果不是這種情況,您必須在比較器中添加邏輯來搜索已排序的整數。

+0

謝謝:) 我做了同樣的方式,它爲我工作 –