4

I have an array and I need to put that array in table.

$scope.testArr=[
    {'first':[
            { 'value':'1_1', 'rolle':'one1' },
            { 'value':'2_1', 'rolle':'two1' },
            { 'value':'3_1', 'rolle':'three1'}  
        ]
    },
    {'second': [
            { 'value':'1_2', 'rolle':'one2' },
            { 'value':'2_2', 'rolle':'two2' },
            { 'value':'3_2', 'rolle':'three2' } 
        ]
    }
];

Resulting table should have 4 columns, each subarray should be one(or two) column(s). Like this:

one1 | 1_1 | one2 | 1-2
two1 | 2_1 | two2 | 2_2   
three1|3_1 | three2|3_2

So far I got this. Its only the first subarray:

<table>
    <tbody ng-repeat="test in testArr">
        <tr ng-repeat="t1 in test.first">
            <td> {{t1.rolle}} </td> 
            <td> {{t1.value}} </td>
        </tr>
    </tbody>
</table>

How can I add the second subarray as column? It's not necessary need to be a table.

1 Answer 1

3

var app = angular.module('app', []);

app.controller('mainCtrl', function ($scope) {


    $scope.testArr = [{
        'first': [{
            'value': '1_1',
            'rolle': 'one1'
        }, {
            'value': '2_1',
            'rolle': 'two1'
        }, {
            'value': '3_1',
            'rolle': 'three1'
        }]
    }, {
        'second': [{
            'value': '1_2',
            'rolle': 'one2'
        }, {
            'value': '2_2',
            'rolle': 'two2'
        }, {
            'value': '3_2',
            'rolle': 'three2'
        }]
    }];

});
td {
  border:solid 1px grey
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app">
    <div ng-controller="mainCtrl">
<table>
    <tbody ng-repeat="test in testArr">
        <tr ng-repeat="t1 in test.first">
            <td>{{t1.rolle}}</td>
            <td>{{t1.value}}</td>
            <td>{{testArr[1].second[$index].rolle}}</td>
            <td>{{testArr[1].second[$index].value}}</td>
        </tr>
    </tbody>
</table>
    </div>
    </div>

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

2 Comments

Thank you very much. I was struggling with indexes all day. U saved me.
What if you don't want to repeat the <tbody> element. So you only want to loop the array and then repeat the <tr> and have one <tbody>. How to do that?

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.