angularjs - How to Order by Scope function and then by a field -


how can order gettotal() , quantity descending?

<!doctype html> <html ng-app="shop">    <head>       <script           src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>    </head>    <body>       <div ng-controller='productctrl'>          <ul>             <li ng-repeat="x in products| orderby:'price'">                {{x.quantity}} {{x.name}}                 <strong>{{ (x.quantity*x.price)|currency}}</strong>             </li>          </ul>          <p>total = {{ (gettotal()) | currency }}</p>       </div>       <script>          var app=angular.module("shop",[])          .controller('productctrl',function($scope){               $scope.products=[{quantity:4,price:42.3,name:"hdd sata"},                      {quantity:14,price:2.83,name:"usb key"},                      {quantity:5,price:11.03,name:"azerty keyboard"},                      {quantity:1,price:4.3,name:"mouse"}];               $scope.gettotal=function(){                   var sum=0;                   for(var i=0;i<$scope.products.length;i++){            sum+=($scope.products[i].price*$scope.products[i].quantity);          }                   return sum;               }          });       </script>    </body> </html> 

if need order gettotal() function return 1 value (total of item values) there no useful of using function order. think need order item totals instead of item total:

try one,

ex: consider following data

 $scope.products = [{   quantity: 4,   price: 1,   name: "hdd sata" }, {   quantity: 2,   price: 2,   name: "usb key" }, {   quantity: 5,   price: 11.03,   name: "azerty keyboard" }, {   quantity: 1,   price: 4.3,   name: "mouse" }]; 

i think need hdd sata on top , azerty keyboard on bottom, because total price hdd sata $4.00 meanwhile usb key $4.00 hdd sata has 4 quantity while usb key has 2 quantity , u need quantity descending hdd sata in top of usb key. azerty keyboard has subtotal of $55.15 highest total item , in bottom.

in html,

<li ng-repeat="x in products| orderby:[gettotalforitem, '-quantity'] "> 

in controller,

$scope.gettotalforitem = function(item) {   return item.price * item.quantity; } 

here demo plunker


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -