0

I just stepped into the AngularJS world, and need a solution for the application I am working on.

So here is the definition of the module.

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

samplesApp.controller('samplesController', function ($scope) {
   var jsonObj=
      [
         {"ACTION":"UPDATE","ID":"22","ROUTE":"0015"},
         {"ACTION":"DELETE","ID":"20","ROUTE":"0015"},
         {"ACTION":"UPDATE","ID":"19","ROUTE":"0015"}
      ]
    $scope.records = jsonObj;

    var columnNames = [];
    for (var key in jsonObj[0]) {
        columnNames.push(key);
    }

    $scope.columnNames = columnNames;

});

The $scope JSON object is a part of the output of the real data that comes from database. and I need to put these data into a table dynamically

The html is like

<table>
    <thead>
        <th data-ng-repeat="column in columnNames">{{column}}</th>
    </thead>
    <tr data-ng-repeat="record in records">
        <td data-ng-repeat="column in columnNames">{{ record.{{ column }} }}</td>
    </tr>
</table>

Because I have no idea what the column name is, so I made a process to get all the column names and push them into $scope.columnNames. and then bind it to the table header. There is no problem for this part. The issue is I don't know how to get the value coresponse to the specific column. I was trying to do it like this:

<td data-ng-repeat="column in columnNames">{{ record.{{ column }} }}</td>

But apparently it is not working.

Can someone give me some advice? really appreciate it.

2
  • Just an FYI, you have an array of records defined on your scope but are referencing AllRecords in your markup Commented Apr 5, 2014 at 0:42
  • Sorry, I forgot to change it when I was extracting the code from my project, I have already updated the post. Thank you for pointing that. Commented Apr 7, 2014 at 17:19

2 Answers 2

2

Try using brackets, like this:

<tr data-ng-repeat="record in AllRecords">
    <td data-ng-repeat="column in columnNames">{{ record[column] }}</td>
</tr>

The expressions inside {{ }} (moustaches?) are evaluated pretty narrowly to how regular javascript is evaluated.

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

1 Comment

Yes, that is it. really appreciate it, Mikke.
1

Accessing with the record[column] operator is OK, but you could try this other alternative. In this example, the semantics of the code is a bit better, and you have less coupling (your rows just depend on the data, not the variable used in the header):

<table>
    <tr>
        <th data-ng-repeat="column in columnNames">{{column}}</th>
    </tr>

    <tr data-ng-repeat="record in records">
        <td data-ng-repeat="(key,value) in record">{{value}}</td>
    </tr>
</table>

Here's the working example with your data: http://plnkr.co/edit/CskLQ2ZZlYIURdNPXiZt?p=preview

Here's the Angular docs for the ngRepeat directive (look for "key, value")

3 Comments

Thanks for the advice, GonchuB, I tried this, and it did give me all the records shows up there, but the value does not match the column name in the <th> very well. But I am 100% sure that this is a very nice solution for parsing the JSON object.
Glad I helped. Just to know, which part didn't work for you? The <th data-ng-repeat="column in columnNames">? Cause I took that one from your code xD
I'll put it this way, let say I use <td data-ng-repeat="(key,value) in record">{{key}}</td>. Notice that I changed "{{value}}" to "{{key}}", when it display, it is not showing the same name as the name on the <th> header. I have no idea how the mismatch happened.

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.