Let's say I have this object:
var rows = [
[1, 2, 3, 4],
[11, 222, 3333, 4444]
];
Given that, and this template:
<tr ng-repeat="row in rows | orderBy ????">
<td ng-repeat="cell in row">{{ cell }}</td>
</tr>
...how can I order an ng-repeat
by the second "column" of each row (the value at index 1
of the given row
element)?
Am I correct that Angular does not support this case—without having to write a custom sort function? (I'm just prototyping, so using ng-init
to define my scope variables instead of creating a controller.)
Actually it does. You can create custom order by functions.
http://plnkr.co/edit/f6hCbHcLkrjyTODvDWjM?p=preview
<div ng-repeat="row in rows | orderBy:secondIndex">
{{row}}
</div>
//In controller
$scope.secondIndex = function(arr){
return arr[1];
}
You just should to use orderBy:'1'
:
<tr ng-repeat="row in rows | orderBy:'1'">
<td ng-repeat="cell in row">{{ cell }}</td>
</tr>
Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With