简体   繁体   中英

AngularJS Filter Sorting map (key,value) list

I am trying to sort a list that I have

  {
    Student: 100,
    Student1: 60,
    Student2: 90,
  }

I am using a table and I need to sort the by their value. It currently sorts by the keys

 <tr ng-repeat="(key, value) in ctrl.students" >

           <td >{{key}}</td>
            <td >{{value}}</td>

   </tr>

I am not sure how to add filtering to make sure it outputs the student scores from highest to lowest.

Basically, you create an array of students with student name and student grade. And you loop through the students array by using ng-repeat and display individual student in descending order by grade. AngularJS orderBy example . The link is an example that using ng-repeat with orderBy .

<tr ng-repeat="student in ctrl.students | orderBy: '-grade' " >
  <td>{{student.name}}</td>
  <td>{{student.grade}}</td>
</tr>

Manipulate data so that it can be use for sorting:

$scope.students = [];
// someData is the data you received from database.
angular.forEach(someData, function(value, key){
  $scope.students.push({
    name: key,
    grade: value
  });
});

And after manipulation, your students array will look like this:

$scope.students = [
 {name: student, grade: 100}, 
 {name: student1, grade: 60}, 
 {name: student2, grade: 90} 
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM