使用ng-repeat和嵌套循环添加行

Adding rows with ng-repeat and nested loop

本文关键字:添加行 嵌套循环 ng-repeat 使用      更新时间:2023-09-26

我正在寻找一种方法来添加行表。我的数据结构是这样的:

  rows = [
    { name : 'row1', subrows : [{ name : 'row1.1' }, { name : 'row1.2' }] },
    { name : 'row2' }
  ];

我想创建一个表,看起来像这样:

  table
     row1
     row1.1
     row1.2
     row2

有可能用angular js ng-repeat吗?如果不是,那么用什么"有棱角"的方式来做呢?

编辑:平坦数组将是一个糟糕的解决方案,因为如果我可以遍历子元素,我可以在单元格内使用不同的html标签,其他css类等。

一年多后,但找到了一个解决方案,至少两个级别(父亲->儿子)。
重复tbody:

<table>
  <tbody ng-repeat="row in rows">
    <tr>
      <th>{{row.name}}</th>
    </tr>
    <tr ng-repeat="sub in row.subrows">
      <td>{{sub.name}}</td>
    </tr>
  </tbody>
</table>

据我所知,所有的浏览器都支持一个表中多个tbody元素

3年多以后,我一直面临着同样的问题,在写下一个指令之前,我尝试了这个,它对我来说很有效:

<table>
    <tbody>
        <tr ng-repeat-start="row in rows">
            <td>
                {{ row.name }}
            </td>
        </tr>
        <tr ng-repeat-end ng-repeat="subrow in row.subrows">
            <td>
                {{ subrow.name }}
            </td>
        </tr>
    </tbody>
</table>

使用ng-repeat将无法做到这一点。不过,你可以用指令来完成。

<my-table rows='rows'></my-table>

小提琴。

myApp.directive('myTable', function () {
    return {
        restrict: 'E',
        link: function (scope, element, attrs) {
            var html = '<table>';
            angular.forEach(scope[attrs.rows], function (row, index) {
                html += '<tr><td>' + row.name + '</td></tr>';
                if ('subrows' in row) {
                    angular.forEach(row.subrows, function (subrow, index) {
                        html += '<tr><td>' + subrow.name + '</td></tr>';
                    });
                }
            });
            html += '</table>';
            element.replaceWith(html)
        }
    }
});

我有点惊讶,这么多人提倡自定义指令和创建代理变量由$watch更新。

像这样的问题就是AngularJS创建过滤器的原因!

From the docs:

A filter formats the value of an expression for display to the user.

我们不希望操纵数据,只是格式化它以不同的方式显示。让我们创建一个过滤器,它接收行数组,使其变平,并返回变平的行。

.filter('flattenRows', function(){
return function(rows) {
    var flatten = [];
    angular.forEach(rows, function(row){
      subrows = row.subrows;
      flatten.push(row);
      if(subrows){
        angular.forEach(subrows, function(subrow){
          flatten.push( angular.extend(subrow, {subrow: true}) );
        });
      }
    });
    return flatten;
}
})

现在我们所需要做的就是将过滤器添加到ngRepeat:

<table class="table table-striped table-hover table-bordered">
  <thead>
    <tr>
      <th>Rows with filter</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="row in rows | flattenRows">
          <td>{{row.name}}</td>
      </tr>
  </tbody>
</table>

如果需要,您现在可以自由地将您的表与其他过滤器组合在一起,例如搜索。

虽然多body方法方便且有效,但它会弄乱任何依赖于子行顺序或索引的css,例如"条纹"表,并且还假设您没有以不希望重复的方式样式化您的body。

这里有一个plunk: http://embed.plnkr.co/otjeQv7z0rifPusneJ0F/preview

编辑:我添加了一个子行值,并在表中使用它来显示哪些行是子行,因为您表示担心能够这样做。

是有可能的:

控制器:

app.controller('AppController',
    [
      '$scope',
      function($scope) {
        $scope.rows = [
          { name : 'row1', subrows : [{ name : 'row1.1' }, { name : 'row1.2' }] },
          { name : 'row2' }
        ];
      }
    ]
  );
HTML:

<table>
  <tr ng-repeat="row in rows">
    <td>
      {{row.name}}
      <table ng-show="row.subrows">
        <tr ng-repeat="subrow in row.subrows">
          <td>{{subrow.name}}</td>
        </tr>
      </table>
    </td>
  </tr>
</table>

砰砰作响

如果您不想要子表,请将行平铺(在注释子行时,以便能够区分):

控制器:

function($scope) {
  $scope.rows = [
    { name : 'row1', subrows : [{ name : 'row1.1' }, { name : 'row1.2' }] },
    { name : 'row2' }
  ];
  $scope.flatten = [];
  var subrows;
  $scope.$watch('rows', function(rows){
    var flatten = [];
    angular.forEach(rows, function(row){
      subrows = row.subrows;
      delete row.subrows;
      flatten.push(row);
      if(subrows){
        angular.forEach(subrows, function(subrow){
          flatten.push( angular.extend(subrow, {subrow: true}) );
        });
      }
    });
    $scope.flatten = flatten;
  });
}
HTML:

<table>
  <tr ng-repeat="row in flatten">
    <td>
      {{row.name}}
    </td>
  </tr>
</table>

恰好

下面是一个例子。此代码打印peopeByCity数组中所有人的所有姓名。

TS:

export class AppComponent {
  peopleByCity = [
    {
      city: 'Miami',
      people: [
        {
          name: 'John', age: 12
        }, {
          name: 'Angel', age: 22
        }
      ]
    }, {
      city: 'Sao Paulo',
      people: [
        {
          name: 'Anderson', age: 35
        }, {
          name: 'Felipe', age: 36
        }
      ]
    }
  ]
}
HTML:

<div *ngFor="let personsByCity of peopleByCity">
  <div *ngFor="let person of personsByCity.people">
    {{ person.name }}
  </div>
</div>