【问题标题】:AngularJS "Vertical" ng-repeatAngularJS“垂直”ng-repeat
【发布时间】:2016-03-22 08:34:47
【问题描述】:

假设我们有一个 JSON 格式的人员数组。每个实体有大约 100 个属性。使用 ng-repeat 的标准方法:

...
<tr>
  <th>Attribute1</th>
  <th>Attribute2</th>
  ...
  <th>AttributeN</th>
</tr>
...
<tr ng-repeat="obj in jsonArray">
  <td>{{ obj.attr1 }}</td>
  <td>{{ obj.attr1 }}</td>
  ...
  <td>{{ obj.attrN }}</td>
</tr>

生成下表:

Attribute1 | Attribute2 | ... | AttributeN
------------------------------------------
value1.1   | value1.2   | ... | value1.N
value2.1   | value2.2   | ... | value2.N
...
valueN.1   | valueN.2   | ... | valueN.N

我需要的是:

Attribute1 | value1.1 | value2.1 | ... | valueN.1
Attribute2 | value1.2 | value2.2 | ... | valueN.2
...        | ...      | ...      | ... | ...
AttributeN | value1.N | value2.N | ... | valueN.N

那么问题来了:我该如何实现呢?

  • 无需“手动”操作 (js-jQuery),无需离开 Angular 世界 - 会有一些事件处理程序等。

【问题讨论】:

  • 为什么不用css.Refer-stackoverflow.com/questions/16918094/…
  • 因为这太难了 - 行中的单元格必须具有相同的高度,+ :hover 行突出显示。而且是手工制作的=)
  • @chip 你能加点小提琴吗?
  • @Y.Puzyrenko - 为了什么?有什么不清楚的地方吗?

标签: angularjs angularjs-ng-repeat


【解决方案1】:

如果我正确理解你想要达到的目标,那么你会这样做:

<table>
  <tr ng-repeat="(key, value) in people[0]">
    <th>{{key}}</th>
    <td ng-repeat="person in people">
      {{person[key]}}
    </td>
  </tr>
</table>

假设您的数据是具有相同属性的对象数组,您遍历数组中的第一个对象以获取键,这将生成垂直表头。

之后,您遍历整个数组并简单地输出特定键的值。这是一个显示输出的小提琴:

http://jsfiddle.net/andreiho/huL8pvmg/1/

当然,如果您想手动定义标题的名称,则必须进行更改。此示例仅获取数据中的键。您还可以在将数据发送到视图之前对其进行操作,因此您只需发送所需的密钥。

【讨论】:

  • 你是我的英雄!你让我的一天 - 很简单 =) 干得好!
  • 但是有一个问题 - 中继器太多。我的意思是:10 个对象具有 100 个属性...此视图只是复杂页面中的一个选项卡...
  • 你试过了吗,它运行缓慢?还是您通常担心使用过多的中继器?我想在这种情况下很难将其保持在最低限度。如果不实际编写自定义过滤器来操作数据,我看不到任何其他方式可以实现这一目标。归根结底,无论如何,您都必须在过滤器中运行某种循环。
  • 我完全同意 - 无论如何它都会循环,似乎没有更好的解决方案。而你的变体(不使用过滤器)更干净、更快……刚刚完成了我的真实示例 - 没有性能问题。非常感谢!
【解决方案2】:

解决此问题的一种方法是将jsonArray 重组为索引对象数据结构。

DEMO

Javascript

.controller('DemoController', function(Data, $scope) {

  Data.all().then(function(response) {
    $scope.indexedObjects = indexByAttribute(response.data);
  });

  function indexByAttribute(collection) {
    return collection.reduce(function(result, item) {

      angular.forEach(item, function(value, index) {
        result[index] = result[index] || [];
        result[index].push(value);
      });

      return result;
    }, {});
  }

});

HTML

<table>
  <tr ng-repeat="(index, values) in indexedObjects track by index">
    <th>{{ index }}</th>
    <td ng-repeat="value in values track by $index">
      {{ value }}
    </td>
  </tr>
</table>

【讨论】:

    【解决方案3】:

    您可以编写自定义filter

    yourModule.filter('myFilter', [ function (arr) {
        var newArr = [];
        // loop over arr and construct newArr as you wish
        ... 
    
        return newArray;
    } ])
    

    像使用它`

    <tr ng-repeat="obj in (jsonArray | myFilter)">
        <td>{{ obj.attr1 }}</td>
        <td>{{ obj.attr2 }}</td>
        ...
        <td>{{ obj.attrN }}</td>
    </tr>
    

    这样 ng-repeat 会将您的 jsonArray 转换为新形成的数组并在循环中使用它,但您的 jsonArray 仍然保持不变,以防您在其他地方使用它。

    【讨论】:

    • 我认为 - 这里与过滤器无关。这都是关于数据表示结构的。
    • 经过一番思考......也许你是对的,这是最好的方法 =) 我会检查一下。谢谢!我会回来的
    • 我觉得以上解决方案更适合我
    猜你喜欢
    • 2014-07-11
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 2017-11-19
    • 2014-09-07
    • 1970-01-01
    相关资源
    最近更新 更多