【问题标题】:ng-repeat-start nested object within a table表中的 ng-repeat-start 嵌套对象
【发布时间】:2015-09-19 03:00:57
【问题描述】:

我正在尝试使用表格以下列格式显示以下信息。

这是一个 plnkr http://plnkr.co/edit/iYEVqUOU3aS4y0t9L1Qa?p=preview

$scope.persons = [
    {
     "name":"Mandy",
     "homes":[
            {
                "location":"California",
                "size":"medium",
                "furniture":[
                    {
                        "name":"couch",
                        "color":"white"
                    },
                    {
                        "name":"table",
                        "color":"black"
                    }
                ]
            }
        ]
      }
  ]

和html

<div ng-controller="ModalDemoCtrl">

<table class="table">
  <thead>
    <th>Name</th>
    <th>House</th>
    <th>Furniture</th>
  </thead>
  <tbody>
    <tr ng-repeat="person in persons">
      <td>{{person.name}}</td>
      <td ng-repeat-start="home in person.homes">
        <ul>
          <li>{{home.location}}</li>
        </ul>  
      </td>
      <td ng-repeat-end>
         <ul ng-repeat="furniture in home.furniture">
          <li>{{furniture.name}}</li>
        </ul> 
      </td>
    </tr>
  </tbody>
</table>


</div>

这适用于 1 个房屋和多件家具,但是当有超过 1 个房屋(以及多件家具)时,我会遇到 ui/table 问题。

我尝试将 ng-repeat-start 移动到 ul 元素,但 angular 抱怨我这样做时无法找到 ng-repeat-end。因此,我不得不重复 td,而不是重复 ul,这会在超过 1 个家时使 ui 混乱。

我又制作了一个 plnkr,这样你就可以看到 2 个房子的样子。

http://plnkr.co/edit/Q37peH968SJc9OvD8lif?p=preview

还有其他方法可以绕过这个 ng-repeat-start / end 限制吗?

【问题讨论】:

  • 问题是您试图在二维网格(表格)中显示 3 维数据(人-家-家具)。在解决这个问题之前,您需要描述(或画出)您希望看到的内容 - 然后我们才能弄清楚如何实现该结果。
  • @Agop 我用图片更新了操作

标签: html angularjs ng-repeat


【解决方案1】:

根据我的评论,问题在于您试图在 2 维网格(表格)中显示 3 维数据(人-家-家具)。

在不展平数据的情况下,解决方案是使用多个表体作为第三维:

  1. 表体 = 人
  2. 行 = 家
  3. 单元格内的列表 = 家具

参见下面的 sn-p 示例。请注意,我们为第一列(人名)提供了等于房屋数量的rowspan,并且我们仅针对该人的第一个房屋显示它。

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

app.controller('PersonHomeFurnitureCtrl', function($scope) {

  $scope.persons = [{
    "name": "Mandy",
    "height": 5,
    "homes": [{
      "location": "California",
      "size": "medium",
      "furniture": [{
        "name": "couch",
        "color": "white"
      }, {
        "name": "table",
        "color": "black"
      }]
    }, {
      "location": "Arizona",
      "size": "large",
      "furniture": [{
        "name": "couch",
        "color": "blue"
      }, {
        "name": "table",
        "color": "light brown"
      }]
    }]
  }]

});
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.js"></script>

<div ng-app="app" ng-controller="PersonHomeFurnitureCtrl">

  <table class="table">
    <thead>
      <th>Name</th>
      <th>House</th>
      <th>Furniture</th>
    </thead>
    <!-- one table body per person -->
    <tbody ng-repeat="person in persons">
      <!-- one row per home -->
      <tr ng-repeat="home in person.homes">
        <td rowspan="{{person.homes.length}}" ng-if="$index === 0">{{person.name}}</td>
        <td>
          <ul>
            <li>{{home.location}}</li>
          </ul>
        </td>
        <td>
          <!-- one list per furniture -->
          <ul ng-repeat="furniture in home.furniture">
            <li>{{furniture.name}}</li>
          </ul>
        </td>
      </tr>
    </tbody>
  </table>

或者,如果您想避免使用多个表格主体,可以将数据展平一维。例如,创建一个homes 数组,并在每个home 中引用原始person。然后,使用与上述相同的逻辑,但使用home.person 来引用人员而不是中继器。

【讨论】:

  • 由于不可能拥有超过 3 维的数据集,我继续将 ng-repeat 附加到正文并按照建议设置行跨度和索引,效果很好。但是,如果 Angular 的起始端不重复 tbody 来完成此任务,那就太好了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多