简体   繁体   English

在表格中使用多个ngrepeat时如何避免多个tbody

[英]how can I avoid multiple tbody when using multiple ngrepeat in table

DEMO: http://jsfiddle.net/6dJ79/1/ 演示: http//jsfiddle.net/6dJ79/1/

The demo above works, however it achieves the results by repeating tbody. 上面的演示有效,但它通过重复tbody实现了结果。 I'm wanting to avoid this. 我想避免这种情况。

Is there any other way that I can have a row for each color, for each person without repeating tbody? 对于每个不重复tbody的人,有没有其他方法可以为每种颜色添加一行? Ideally I'm wanting to have the output look like below without manipulating the data. 理想情况下,我希望在不操作数据的情况下让输出看起来如下所示。

<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Color</th>
            <th>Fav</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Bill</td>
            <td>red</td>
            <td>yes</td>
        </tr>
        <tr>
            <td>Bill</td>
            <td>blue</td>
            <td>no</td>
        </tr>
        <tr>
            <td>Larry</td>
            <td>purple</td>
            <td>yes</td>
        </tr>
        <tr>
            <td>Larry</td>
            <td>yellow</td>
            <td>no</td>
        </tr>
    </tbody>
</table>

The data: 数据:

{
    name: 'Bill',
    colors: [{
        name: 'red',
        favColor: 'yes'
    }, {
        name: 'blue',
        favColor: 'no'
    }]
}, {
    name: 'Larry',
    colors: [{
        name: 'purple',
        favColor: 'yes'
    }, {
        name: 'yellow',
        favColor: 'no'
    }]
}

Here is an implementation using filter. 这是使用过滤器的实现

myApp.filter('denormalize', function(){
    return function(people) {
        var arr = [];
        angular.forEach(people, function(person){
            angular.forEach(person.colors, function(color){
                arr.push({
                    name: person.name, 
                    color: color.name, 
                    fav: color.favColor
                });
            });
        });

        return arr;
    }
});

In html: 在html中:

<tbody>
    <tr ng-repeat="person in people | denormalize">
        <td>{{ person.name }}</td>
        <td>{{ person.color }}</td>
        <td>{{ person.fav }}</td>
    </tr>
</tbody>

I would create a function on your scope which would flatten your nested objects into a simple flat array using nested loops. 我会在你的作用域上创建一个函数,它会使用嵌套循环将嵌套对象展平为一个简单的平面数组。

Here is a working fiddle Scope method: 这是一个工作小提琴范围方法:

$scope.peopleColors = function () {
    var pc = [];
    for (var i = 0, plen = $scope.people.length; i < plen; i++) {
        var person = $scope.people[i];
        for (var j = 0, clen = person.colors.length; j < clen; j++) {
            var color = person.colors[j];
            pc.push({
                name: person.name,
                color: color.name,
                favColor: color.favColor
            });
        }
    }
    return pc;
};

Markup: 标记:

<tr ng-repeat="pc in peopleColors()">

Edit: I updated the fiddle to show dynamic adding of a new person 编辑:我更新了小提琴,以显示动态添加新人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM