Ng-repeat in an array of arrays, orderBy swap index

Let's say I have this object:

var rows = [ [1, 2, 3, 4], [11, 222, 3333, 4444] ]; 

Given this, and this template:

 <tr ng-repeat="row in rows | orderBy ????"> <td ng-repeat="cell in row">{{ cell }}</td> </tr> 

... how can I order ng-repeat second "column" of each row (the value at index 1 this row element)?

I will fix that Angular does not support this case - without having to write a custom sort function? (I'm just prototyping, so I use ng-init to define scope variables instead of creating a controller.)

+6
source share
2 answers

Actually it is. You can create your own order by function.

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]; } 
+3
source

You just need to use orderBy:'1' :

 <tr ng-repeat="row in rows | orderBy:'1'"> <td ng-repeat="cell in row">{{ cell }}</td> </tr> 

Demo

+2
source

Source: https://habr.com/ru/post/989767/


All Articles