Search code examples
javascriptangularjshtml-tableangularjs-ng-repeat

How to build table with rowspan from array of objects in AngularJS


I have similar problem as in this question: AngularJS repeat with table and rowspan.

I want to build table with rowspan from object.

But I use array of objects instead of single object as data for table.

Example:

[
    {
        "key": "key 1",
        "values": [
                {
                    "value": "value-1"
                },
                {
                    "value": "value-2",
                }
            ]
        },
        {
            "key": "key 2",
            "values": [
                {
                    "value": "value-3"
                }
            ]
        }

instead of

{
    key1:[1,2],
    key2:[3,4,5]
}

As a result I want to transform this array:

[
    {
        "login": "Affiliate 1",
        "referrals": [
            {
                "login": "referral-1",
                "bonusAmount": 100.00
            },
            {
                "login": "referral-2",
            }
        ]
    },
    {
        "login": "Affiliate 2",
        "referrals": [
            {
                "login": "referral-3",
                "bonusAmount": 300.00
            }
        ]
    },
    {
        "login": "Affiliate 3",
        "referrals": [
            {}
        ]
    }
]

to table:

<table border="1">
  <tr>
    <th scope="col">Affiliate name</th>
    <th scope="col">Referral name</th>
    <th scope="col">Affiliate bonus</th>
  </tr>
  <tr>
    <td rowspan="2">Affiliate 1</td>
    <td>referral-1</td>
    <td>$100.00</td>
  </tr>
  <tr>
    <td>referral-2</td>
    <td></td>
  </tr>
  <tr>
    <td>Affiliate 2</td>
    <td>referral-3</td>
    <td>$300.00</td>
  </tr>
  <tr>
    <td>Affiliate 3</td>
    <td></td>
    <td></td>
  </tr>
</table>


Solution

  • One option is to put the main loop over the tbody element in the table ng-repeat="item in items" and then use a nested loop over each tr ng-repeat="ref in item.referrals" where you put the rowspan condition on the first td <td ng-if="$index == 0" rowspan={{item.referrals.length}}>{{item.login}}</td>.

    Here's a working example

    var app = angular.module('app', []);
    app.controller('ctrl', function ($scope) {
       $scope.items = [
        {
            "login": "Affiliate 1",
            "referrals": [
                {
                    "login": "referral-1",
                    "bonusAmount": 100.00
                },
                {
                    "login": "referral-2",
                }
            ]
        },
        {
            "login": "Affiliate 2",
            "referrals": [
                {
                    "login": "referral-3",
                    "bonusAmount": 300.00
                }
            ]
        },
        {
            "login": "Affiliate 3",
            "referrals": [
                {}
            ]
        }
      ];
      
    });
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    
    <div ng-app="app" ng-controller="ctrl">
    
    
    <table border="1">
      <tr>
        <th>Affiliate name</th>
        <th>Referral name</th>
        <th>Affiliate bonus</th>
      </tr>
      <tbody ng-repeat="item in items">
        <tr ng-repeat="ref in item.referrals">
          <td ng-if="$index == 0" rowspan={{item.referrals.length}}>{{item.login}}</td>
          <td>{{ref.login}}</td>
          <td>{{ref.bonusAmount > 0 ? '$' + ref.bonusAmount:''}}</td>
        </tr>
      </tbody>
    </table>
    
    </div>