Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-repeat on array of arrays, orderBy subarray index

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.)

like image 691
core Avatar asked Jun 25 '15 20:06

core


Video Answer


2 Answers

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];
}
like image 107
Devin H. Avatar answered Oct 30 '22 16:10

Devin H.


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

like image 33
Walter Chapilliquen - wZVanG Avatar answered Oct 30 '22 15:10

Walter Chapilliquen - wZVanG