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

标签 javascript html angularjs angularjs-ng-repeat ng-repeat

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

上面的演示有效,但是它通过重复 tbody 来实现结果。我想避免这种情况。

有没有其他方法可以让每个人的每种颜色各占一行,而无需重复 tbody?理想情况下,我希望在不处理数据的情况下输出如下所示。

<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>

数据:

{
    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使用过滤器。

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;
    }
});

在 html 中:

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

关于javascript - 在表中使用多个 ngrepeat 时如何避免多个 tbody,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22926358/

相关文章:

javascript - 调整可见性时 div 元素闪烁

javascript - float ajax 内容未按需要包装

javascript - Angularjs 中的 $q.all() 多次调用

javascript - 为什么结果字符串只返回最后一个结果值

javascript - swisnl 的 jquery contextMenu 根据单击的元素将自定义数据添加到名称

javascript - Laravel 使用 Ajax 以一种形式更新数据和文件?当方法/类型为='PUT'时

javascript - 在使用 Bootstrap 3 选项卡更改选项卡之前如何要求确认?

javascript - AngularJS 从 JSON 读取内容不返回任何内容

javascript - AngularJS 的全局 Ajax 错误处理程序

javascript - AngularJs 允许仅摘要当前范围