2

I have dynamic inputData array (length can be between 0 and 10 or 15) that looks something like this:

$scope.inputData = [
    [1, 2, 3, 4, 5, 6, 7];
    [1, 2, 3, 4, 5, 6, 7];
    [1, 2, 3, 4, 5, 6, 7];
    [1, 2, 3, 4, 5, 6, 7];
    [1, 2, 3, 4, 5, 6, 7];
    [1, 2, 3, 4, 5, 6, 7];
]

I need to generate td in the table with ng-repeat so I could have an only first item from each "sub-array" in the first iteration. Then only second item from each "sub-array", etc.. So table should be

th   th   th  th  th  th
1    1    1   1   1   1
2    2    2   2   2   2
3    3    3   3   3   3
...

And ng repeat should switch indexes in each iteration.

// first iteration: 
<td ng-repeat="item in inputData track by $index">{{item[0]}}</td>
// second iteration: 
<td ng-repeat="item in inputData track by $index">{{item[1]}}</td>
...

Can I somehow solve this only using ng-repeat or should I sort my arrays in the controller?

0

2 Answers 2

2

Try this:

angular.module('app', []).controller('ctrl', function($scope) {
  $scope.inputData = [
    [1, 2, 3, 4, 5, 6, 7],
    [1, 2, 3, 4, 5, 6, 7],
    [1, 2, 3, 4, 5, 6, 7],
    [1, 2, 3, 4, 5, 6, 7],
    [1, 2, 3, 4, 5, 6, 7],
    [1, 2, 3, 4, 5, 6, 7]
  ]
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js">
</script>

<table ng-app='app' ng-controller='ctrl'>
  <tbody>
    <tr ng-repeat='array in inputData' ng-init='parentIndex=$index'>
      <td ng-repeat='item in array'>{{array[parentIndex]}}
        <td>
    </tr>
  </tbody>
</table>

Sign up to request clarification or add additional context in comments.

9 Comments

Doesn't work. Now got an entire array in a row. '1, 2, 3, 4, 5'
@MatijaŽupančić, are you sure, check out code snippet?
@MatijaŽupančić, post your data, on which it is not work.
For the given solution, row number should match column number then only the entire array will be displayed. In above case, row no.= 6 and column no.= 7. So, we cannot get the entire array as the result.
I've got some array before so I have solved in this way. <tr ng-repeat="row in input.inputBuckets track by $index"> <td>{{row.key | date:'yyyy-MM-dd HH:mm:ss'}}</td> <td ng-repeat="item in input.inputData" >{{item[$parent.$index]}}</td> </tr> These two arrays have the same length. And now works fine. Thanks for your help anyway.
|
0

 <!DOCTYPE html>
    <html ng-app="app">
    
      <head>
        <meta charset="utf-8" />
        <title>AngularJS Example</title>
        <script data-require="[email protected]" src="https://code.angularjs.org/1.4.3/angular.js" data-semver="1.4.3"></script>
        <script>
          var app = angular.module('app', []);
          app.controller('MainCtrl', function($scope) {
            var items = [
        [1, 2, 3, 4, 5, 6, 7],
        [1, 2, 3, 4, 5, 6, 7],
        [1, 2, 3, 4, 5, 6, 7],
        [1, 2, 3, 4, 5, 6, 7],
        [1, 2, 3, 4, 5, 6, 7],
        [1, 2, 3, 4, 5, 6, 7]
            ];
            
           $scope.items_transpose = transpose(items);
    
            function transpose(a) {
    
      // Calculate the width and height of the Array
      var w = a.length || 0;
      var h = a[0] instanceof Array ? a[0].length : 0;
    
      // In case it is a zero matrix, no transpose routine needed.
      if(h === 0 || w === 0) { return []; }
    
      /**
       * @var {Number} i Counter
       * @var {Number} j Counter
       * @var {Array} t Transposed data is stored in this array.
       */
      var i, j, t = [];
    
      // Loop through every item in the outer array (height)
      for(i=0; i<h; i++) {
    
        // Insert a new row (array)
        t[i] = [];
    
        // Loop through every item per item in outer array (width)
        for(j=0; j<w; j++) {
    
          // Save transposed data.
          t[i][j] = a[j][i];
        }
      }
    
      return t;
    }
            
          });
        </script>
      </head>
      <body ng-controller="MainCtrl">
        
        <table>
            <tr ng-repeat="item in items_transpose track by $index">
                <td ng-repeat="i in item track by $index">{{i}}</td>
            </tr>
        </table>
        
      </body>
    </html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.