【问题标题】:AngularJS to generate grouped data in a table using RowSpanAngularJS 使用 RowSpan 在表中生成分组数据
【发布时间】:2017-06-29 03:19:57
【问题描述】:

如果我有以下数据结构:

data = [{"nums": [1, 6, 5], "name": "John"},
        {"nums": [2], "name": "Bob"},
        {"nums": [9, 6], "name": "Jason"}]

我希望它使用 ng-repeat 输出到 html 为:

|------------|
| 1 |        |
|---|        |
| 6 | John   |
|---|        |
| 5 |        |
|---|--------|
| 2 | Bob    |
|---|--------|
| 5 |        |
|---| Jason  |
| 2 |        |
|---|--------|

我该怎么做?

【问题讨论】:

标签: javascript angularjs html-table ng-repeat


【解决方案1】:

您可以在 tbody 上使用 ng-repeat,然后像这样继续循环

<table>
  <tbody ng-repeat="row in data">
    <tr ng-repeat="col in row.nums">
      <td>{{col}}</td>
      <td rowspan="{{$first ? row.nums.length : 1}}" ng-show="$first">{{row.name}}</td>
    </tr>
  </tbody>
</table>

Se plunker here

【讨论】:

    【解决方案2】:

    试试这个:

    var myApp = angular.module('myApp',[]);
    
    myApp.controller('MyCtrl',function($scope) {
        $scope.data = [{"nums": [1, 6, 5], "name": "John"},
            {"nums": [2], "name": "Bob"},
            {"nums": [9, 6], "name": "Jason"}];
    });
    th,td {
      border: 1px solid black;
    }
    
    .tdcell {
      border:  0px;
      }
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app="myApp" ng-controller="MyCtrl">
    <table>
    <thead>
          <tr>
            <th>Name</th>
            <th>Nums</th>
          </tr>
        </thead>
        <tbody ng-repeat='item in data'>
            <td class="spanRows" rowspan="{{item.nums.length}}">{{item.name}}</td>
        <td>
        <table>
          <tbody>
            <tr ng-repeat='num in item.nums'>
              <td class="tdcell">{{num}}</td>
            </tr>
          </tbody>
        </table>
        </td>
            </tbody>
    </table>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-20
      • 2023-03-19
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      相关资源
      最近更新 更多