1

iam new on angularjs and reading the doc about orderBy , wanting to know how to get the max value on certain column,

like the oldest person is Adam - 35

Name (^)    Phone Number    Age
Adam         555-5678       35
Julie        555-8765       29
Mike         555-4321       21
Mary         555-9876       19
John         555-1212       10

the example on plunker

does anyone know how to do it?

thank you

4
  • orderBy is to sort the entries of a collection. It has got nothing to do with finding the max and min values. You either create a custom filter that will give you the max value or simply use regular old javascript to find it yourself. Commented Jan 30, 2015 at 12:22
  • like get the max data from the controller side? i'll try it Commented Jan 30, 2015 at 12:26
  • ordeyBy doesn't manipulate the original model ,its just for view purpose i think you need to sort your object array in controller and then pick the max Commented Jan 30, 2015 at 12:46
  • @user716409 yes correct. Get it from the controller by iterating over the collection Commented Jan 31, 2015 at 3:19

1 Answer 1

2

I hope this is what you are looking for:

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
  $scope.friends = [
    {name:'John', phone:'555-1212', age:10},
    {name:'Mary', phone:'555-9876', age:19},
    {name:'Mike', phone:'555-4321', age:21},
    {name:'Adam', phone:'555-5678', age:35},
    {name:'Julie', phone:'555-8765', age:29}
  ];
  $scope.change = function(o) {
    var arr = [];
    angular.forEach($scope.friends, function(item) {
      arr.push(item[o]);
    });
    $scope.result = arr.sort()[0];
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="MyCtrl">
  <select ng-model="val" ng-change="change(val)">
    <option>name</option>
    <option>phone</option>
    <option>age</option>
  </select>
  <p>sorted first value: {{result}}</p>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.