如何使用 AngularJS 中对象数组的 rowspan 构建 table

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

我遇到了与这个问题类似的问题:AngularJS repeat with table and rowspan

我想用对象的行跨度构建 table。

但我使用对象数组而不是单个对象作为 table 的数据。

示例:

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

而不是

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

因此我想转换这个数组:

[
    {
        "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": [
            {}
        ]
    }
]

到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>0.00</td>
  </tr>
  <tr>
    <td>referral-2</td>
    <td></td>
  </tr>
  <tr>
    <td>Affiliate 2</td>
    <td>referral-3</td>
    <td>0.00</td>
  </tr>
  <tr>
    <td>Affiliate 3</td>
    <td></td>
    <td></td>
  </tr>
</table>

一个选择是将主循环放在 table ng-repeat="item in items" 中的 tbody 元素上,然后在每个 tr ng-repeat="ref in item.referrals" 上使用嵌套循环,您将 rowspan 条件放在第一个 td <td ng-if="$index == 0" rowspan={{item.referrals.length}}>{{item.login}}</td>.

这是一个工作示例

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>