Search code examples
angularjshtmlangularjs-orderby

OrderBy math column in AngularJS


I have a table like this:

<tr ng-repeat="x in data | orderBy:xxxx"> <td>{{ x.name}}</td> <td>{{ x.price * x.count }}</td> </tr>

How can I order this table row by {{ x.price * x.count }} column.

Thank you for your help.


Solution

  • orderBy cab accept not only a value, but a function as well. In order to use it in your case, add a function to your scope:

    $scope.calculateOrder = function(item) {
        return item.price * item.count;
    };
    

    And then you can use this function in your filter:

    <tr ng-repeat="x in data | orderBy:calculateOrder">
    

    You can read more about orderBy on Angular documentation