1

I have a table using angularjs where I want to loop through an array to print specific headers from a json object. The header prints out fine, but the problem comes when I try to use a variable from my nested ng-repeat as a json selector. If you replace the inner ng-repeat with the commented section below it, it will work.

Table:

<table>
    <thead>
        <th ng-repeat="column in tableHeader">{{column}}&nbsp;<a ng-click="sort_by(column);"><i class="glyphicon glyphicon-sort"></i></a></th>
    </thead>
    <tbody>
        <tr ng-repeat="data in filtered>
            <td ng-repeat="column2 in tableHeader">{{data.column2}}</td>
<!-- <td>{{data.Environment}}</td>
    <td>{{data.HostIP}}</td>
    <td>{{data.ServiceName}}</td>
    <td>{{data.Status}}</td>
    <td>{{data.StartTime}}</td>
    <td>{{data.Capacity}}</td>
    <td>{{data.Txn}}</td>
    <td>{{data.Errors}}</td>
    <td>{{data.Build}}</td>
    <td>{{data.Project}}</td>
    <td>{{data.Author}}</td>
    <td>{{data.ModifyDate}}</td>
    <td>{{data.Port}}</td>
    <td>{{data.BasePath}}</td> -->
        </tr>
    </tbody>
</table>

Array located in controller:

$scope.tableHeader = ['Environment', 'HostIP', 'Status', 'ServiceName', 'StartTime', 'Capacity', 'Txn', 'Errors', 'Build', 'Project', 'Author', 'ModifyDate', 'Port', 'BasePath'];
0

2 Answers 2

2

I think you're looking for {{data[column2]}}. Since column2 is just the string value of the property you want, treat data like an associative array in this case to get the property you're trying to display.

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

1 Comment

This will do it, I think.
0

column2 was created by the ng-repeat and is what you want. Note {{column2}}:

<td ng-repeat="column2 in tableHeader">{{column2}}</td>

1 Comment

would output all the headings, not what OP is expecting. Note the nested ng-repeat of td inside repeating tr

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.