Shikha thakur Shikha thakur - 3 months ago 8
Javascript Question

Sort table based on ratings

I have a scenario where i need to sort table based on ratings for which i am using

orderBy
filter

here i could sort by name perfectly ,but i am unable to sort ratings which is again an array

html:

<table class="friends">
<thead>
<tr class='table-head'>
<th scope="col"><a style="cursor:pointer" ng-click="sortType = 'name'; sortReverse = !sortReverse">Candidate Name</a></th>
<th scope="col" ng-repeat="a in [1,2,3,4]" ><a style="cursor:pointer" ng-click="sortType = 'rating'; sortReverse = !sortReverse">Round{{a}}</a></th>

</tr>
</thead>
<tbody>
<tr ng-repeat="candidate in aCandidateDetails| orderBy:sortType:sortReverse" class="text-center">
<td>
<div>{{candidate.name}}</div>
</td>
<td ng-repeat="candidateData in candidate.ratings| orderBy:sortType:sortReverse">
<div >
{{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.sortType = 'name'; // set the default sort type
$scope.sortReverse = false; // set the default sort order


I have done a working example as well which has json data
DEMO

Answer

ngRepeat directive creates a child $scope, so you should use the Dot Rule or controller-as-syntax:

$scope.model = {};
$scope.model.sortType = 'name';
$scope.model.sortReverse = false;

And modify all those in your view..

See the DEMO

Comments