2

I need to create a table which is ordered based on the given attribute. It does not work this way. How can i solve it?

$scope.values = {
        "sales" : [ {
        "jeans" : {
            "quantity" : "163811",
            "order" : 2
        },
        "shoes" : {
            "quantity" : "101",
            "order" : 1
        },
        "trousers" : {
            "quantity" : "10733",
            "order" : 3
        }]
    };

And the view:

<table>
 <tr ng-repeat="item in values | orderBy:'order'">
  <td>{{item.jeans.quantity}}</td>
  <td>{{item.shoes.quantity}}</td>
  <td>{{item.trousers.quantity}}</td>
 </tr>
</table>

How should order by be orderBy:'item.[what_is_needed].order'

thanks

aAnduel
  • 21
  • 2
  • 1
    This repeater is wrong, the key `jeans ` is not a key of `values object`, rather is a key of `sales object`. and this object has syntax error. Please check your object. – ferhado Oct 26 '17 at 13:19
  • It is just an example. Key means what should be there to make it happen :D – aAnduel Oct 26 '17 at 13:28
  • Even if it is an example, providing wrong code makes it a lot harden for people to help you, because it is not clear if these errors might be also part of your problem. – scipper Nov 26 '17 at 07:04

1 Answers1

0

For this array:

$scope.products = [
    {
        item: {
           quantity: 163811
        }
    }, {
        item: {
           quantity: 150
        }
    }, {
        item: {
           quantity: 350
        }
    }
];

You can use the following syntax:

<tr ng-repeat="product in products | orderBy:'item.quantity':true">

See the full answer here

Lucky_hunter
  • 486
  • 1
  • 5
  • 10