1

I am currently trying to display array data in a table using angular. Here is my HTML:

            <table>
            <thead>
                <tr>
                    <th>Location</th>
                    <th>User</th>
                    <th>Date</th>
                    <th>Time</th>
                    <th>Amount</th>
                    <th>Transaction Type</th>
                    <th>Direct Object</th>

                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="user in index.users">
                    <td ng-repeat="transaction in users.transactions">
                     {{transaction.transLocale}}</td>
                    <td>{{user.name}}</td>
                    <td>{{user.transDate}}</td>
                    <td>{{user.transTime}}</td>
                    <td>{{user.transAmount}}</td>
                    <td>{{user.transType}}</td>
                    <td>{{user.directObject}}</td>
                </tr>
            </tbody>
        </table>

And here is my js:

app.controller('IndexController', function(){
    this.users = userHistory;
}); 



var userHistory = [
{
    name: 'Bobby',
    transactions: 
     [{
        transDate: '1/15/15',
        transTime: '4:30pm',
        directObject: 'Matt',
        transAmount: '11',
        transType: 'debt',
        transLocale: 'B-dubs'
    },{
        transDate: '1/12/15',
        transTime: '7:30pm',
        directObject: 'Matt',
        transAmount: '9',
        transType: 'credit',
        transLocale: 'MickDs'       
        }]
 },

{   name: 'Jake',
    transactions: [
     {
        transDate: '1/11/15',
        transTime: '1:30pm',
        directObject: 'Matt',
        transAmount: '5',
        transType: 'credit',
        transLocale: "Jason's"
    }, {
        transDate: '1/12/15',
        transTime: '7:30pm',
        directObject: 'Matt',
        transAmount: '9',
        transType: 'debt',
        transLocale: 'MickDs'       
    }]
},
    {
    name: 'Clayton',
    transactions: {
        transDate: '1/14/15',
        transTime: '2:30pm',
        directObject: 'Matt',
        transAmount: '15',
        transType: 'credit',
        transLocale: "Chen's"
    }
}];

I can't seem to print the data inside of the transactions array? I am just starting out in angular so I am not quite sure what is going on. Any insight would be wonderful, I am trying to learn more than trying to get it to work. Thanks!

2
  • You are printing out {{user.transDate}} whereas it's an attribute of transaction {{transaction.transDate}}. Change others similarly Commented Jan 8, 2015 at 17:24
  • When I printed out transaction.transLocale it inserted the user names, not the location data Commented Jan 8, 2015 at 17:28

3 Answers 3

1

Bind the first ng-repeat to the tbody:

<tbody ng-repeat="user in users">
    <tr ng-repeat="transaction in user.transactions">
        <td>{{transaction.transLocale}}</td>
        <td>{{user.name}}</td>
        <td>{{transaction.transDate}}</td>
        <td>{{transaction.transTime}}</td>
        <td>{{transaction.transAmount}}</td>
        <td>{{transaction.transType}}</td>
        <td>{{transaction.directObject}}</td>
    </tr>
</tbody>

It is okay to have multiple tbody elements in your table.

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

1 Comment

" change users.transactions in user.transactions in your view." what do you mean by this?
0

It will help you find the plunker I made:- http://plnkr.co/edit/1GhzyeszaeGPAC2cu4ib?p=preview

<tbody>
  <tr ng-repeat="user in index.users">
    <td>
      <span ng-repeat="transaction in user.transactions">
        {{transaction.transLocale}}
        <br>
      </span>
    </td>
    <td>{{user.name}}</td>
    <td>
      <span ng-repeat="transaction in user.transactions">
        {{transaction.transDate}}
        <br>
      </span>
    </td>
    <td>
      <span ng-repeat="transaction in user.transactions">
        {{transaction.transLocale}}
        <br>
      </span>
    </td>
    <td>
      <span ng-repeat="transaction in user.transactions">
        {{transaction.transTime}}
        <br>
      </span>
    </td>
    <td>
      <span ng-repeat="transaction in user.transactions">
        {{transaction.transAmount}}
        <br>
      </span>
    </td>
    <td>
      <span ng-repeat="transaction in user.transactions">
        {{transaction.transType}}
        <br>
      </span>
    </td>
    <td>
      <span ng-repeat="transaction in user.transactions">
        {{transaction.directObject}}
        <br>
      </span>
    </td>
  </tr>
</tbody>

Comments

0

Try this

<tbody>
  <tr ng-repeat="user in users">
    <table>
      <tbody>
        <tr ng-repeat="transaction in user.transactions">
          <td>{{transaction.transLocale}}</td>
          <td>{{user.name}}</td>
          <td>{{transaction.transDate}}</td>
          <td>{{transaction.transTime}}</td>
          <td>{{transaction.transAmount}}</td>
          <td>{{transaction.transType}}</td>
          <td>{{transaction.directObject}}</td>
        </tr>
      </tbody>
    </table>
  </tr>
</tbody>

Script:

app.controller('IndexController', function(){
    $scope.users = userHistory;
}); 

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.