Shikha thakur Shikha thakur - 1 year ago 55
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


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


<table class="friends">
<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 ng-repeat="candidate in aCandidateDetails| orderBy:sortType:sortReverse" class="text-center">
<td ng-repeat="candidateData in candidate.ratings| orderBy:sortType:sortReverse">
<div >
<span ng-if="!candidateData.rating"> - NA - </span> </td>
<td data-title="Status">
<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>


$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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download